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
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.
A state of the board can be represented by a string S using the rule showed below.
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.
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.
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.
#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 ;
}