【HDOJ3567】【预处理bfs+映射+康拓展开hash】

http://acm.hdu.edu.cn/showproblem.php?pid=3567

Eight II

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 4541    Accepted Submission(s): 990

Problem Description
Eight-puzzle, which is also called "Nine grids", comes from an old game.

In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.

We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.

【HDOJ3567】【预处理bfs+映射+康拓展开hash】

A state of the board can be represented by a string S using the rule showed below.

【HDOJ3567】【预处理bfs+映射+康拓展开hash】

The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.

 
Input
The first line is T (T <= 200), which means the number of test cases of this problem.
The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.
 
Output
For each test case two lines are expected.
The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
 
Sample Input
2
12X453786
12345678X
564178X23
7568X4123
 
Sample Output
Case 1: 2
dd
Case 2: 8
urrulldr
题意:强化版本的八数码问题。(数据量T很大..而且要求输出最小字母序的操作方式)
题目分析:对于普通的八数码问题可以使用双向bfs解决,这个强化版本由于数据量过大,使用双向bfs可能也会TLE,而且使用双向bfs时反向bfs的过程不容易按最小字母序输出。
题解:由于起始状态与结束状态只是反映了一堆字母以及空格的相对位置关系,和字母本身的含义无关,所以可以预处理跑出空格X在不同位置下到达所有状态所用的最少操作方 式,然后按照每组数据给出的状态进行映射输出即可。这个过程仍然使用康拓展开进行hash。【注意:对string进行的操作真的很慢,所以康拓展开时使用int存状态,不用string】
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<string>
using namespace std;
string str1, str2;
bool vis[];
int dx[] = { ,,,- };
int dy[] = { ,-,, };
char cs[] = { 'd','l','r','u' };
int pre[][];
int op[][];
int jc[];
int kt(int s) //康托展开
{
int code = ;
int st[];
for (int i = ; i >= ; i--, s /= )
st[i] = s % ;
for (int i = ; i<; i++)
{
int cnt = ;
for (int j = i + ; j<; j++)
if (st[j]<st[i]) cnt++;
code += jc[ - i] * cnt;
}
return code;
}
int skt = ;
int mypow(int x, int y) {
int ans = ;
while (y) {
if (y & )ans *= x;
x *= x;
y /= ;
}
return ans;
}
void bfs(string str,int x) {
memset(vis, , sizeof(vis));
queue<int>pq;
queue<int>pq2;
queue<int>pq3;
while (!pq.empty()) {
pq.pop();
}
while (!pq3.empty()) {
pq3.pop();
}
while (!pq2.empty()) {
pq2.pop();
}
int tmps = ;
for (int i = ; i < ; i++) {
tmps = tmps * + str[i] - '';
}
pq.push(tmps);
pq2.push(x);
int kt000 = kt(tmps);
pq3.push(kt000);
vis[kt000] = ;
while (!pq.empty()) {
int str0 = pq.front(); pq.pop();
//cout << str0 << endl;
int s0 = pq2.front(); pq2.pop();
int kt010 = pq3.front(); pq3.pop();
for (int i = ; i < ; i++) {
int x0 = s0 / ;
int y0 = s0 % ;
int tx = x0 + dx[i];
int ty = y0 + dy[i];
int s00 = tx * + ty;
if (tx >= && ty >= && ty < && tx < ) {
int str00=str0;
int skt1 = ((str0) / (mypow(, ( - s0)))) % ;
str00 -= skt1*mypow(,(-s0));
int skt2= ((str0) / (mypow(, ( - s00)))) % ;
str00 += skt2 * mypow(,(-s0));
str00 -= skt2 * mypow(, ( - s00));
str00 += skt1 * mypow(, ( - s0));
//str00[s00] = str0[s0];
int kt0 = kt(str00);
//skt++;
// cout << skt << endl;
// cout << kt0 << endl;
if (!vis[kt0]) {
vis[kt0] = ;
op[x][kt0] = i;
pre[x][kt0] = kt010;
pq.push(str00);
pq2.push(s00);
pq3.push(kt0);
}
}
}
} }
int main() {
int t;
jc[] = ;
for (int i = ; i < ; i++) {
jc[i] = jc[i - ] * i;
}
int case1 = ;
string str[];
str[] = "";
bfs(str[], );
// cout << "%%%%%\n";
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
scanf("%d", &t);
while (t--) {
cin >> str1 >> str2;
int u;
for (int i = ; i < ; i++) {
if (str1[i] == 'X') {
str1[i] = '';
u = i;
}
if (str2[i] == 'X') {
str2[i] = '';
}
}
char hash0[];
for (int i = ; i < ; i++) {
hash0[str1[i] - ''] = str[u][i];
}
string tmp = "";
for (int i = ; i < ; i++) {
tmp += hash0[str2[i] - ''];
}
int s1=, s2=;
for (int i = ; i < ; i++) {
s1 = s1 * + str[u][i] - '';
}
for (int i = ; i < ; i++) {
s2 = s2 * + tmp[i] - '';
}
int sta = kt(s1);
int en = kt(s2);
stack<int>stk;
while (!stk.empty())stk.pop();
while (sta != en) {
stk.push(en);
en = pre[u][en];
}
printf("Case %d: %d\n", case1++, stk.size());
while (!stk.empty()) {
int sss = stk.top();
stk.pop();
if (sss != sta) {
printf("%c",cs[op[u][sss]]);
}
}
printf("\n");
}
return ;
}
上一篇:基于 Webhooks gitlab 自动化构建


下一篇:jqueryui datepicker refresh