Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Special Judge
Problem Description
The 15-puzzle has been around for over 100 years; even if you don’t know it by that name, you’ve seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let’s call the missing tile ‘x’; the object of the puzzle is to arrange the tiles so that they are ordered as:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 x
where the only legal operation is to exchange ‘x’ with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->
The letters in the previous row indicate which neighbor of the ‘x’ tile is swapped with the ‘x’ tile at each step; legal values are ‘r’,’l’,’u’ and ‘d’, for right, left, up, and down, respectively.
Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing ‘x’ tile, of course).
In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.
Input
You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus ‘x’. For example, this puzzle
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
You will print to standard output either the word “unsolvable”, if the puzzle has no solution, or a string consisting entirely of the letters ‘r’, ‘l’, ‘u’ and ‘d’ that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line. Do not print a blank line between cases.
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
问题分析
从目标状态(1 2 3 4 5 6 7 8 0)开始逆向BFS,预处理出每一种状态,最多9!/2种。然后O(1)输出就好了。
解释一下为什么要用康托,因为数组是存不下所有排列的,所以我们用康托展开计算出每个排列的位置,这样大大压缩了空间。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int dx[] = {-1,1,0,0},dy[] = {0,0,-1,1},N = 4e5;
int fac[]={1,1,2,6,24,120,720,5040,40320};
vector<char> path[N];
bool vis[N];
struct node{
int a[9],loc,cantor;//loc是0的位置,cantor是康托展开的排列位置
vector<char> path;//保存路径
};
int Cantor(int *s,int n){//康托展开
int sum=0;
for(int i=0;i<9;i++)
{
int num=0;
for(int j=i+1;j<9;j++)
if(s[j]<s[i])num++;
sum+=(num*fac[9-i-1]);
}
return sum+1;
}
char dir[] = "durl";
void bfs()
{
memset(vis,0, sizeof(vis));
queue<node> q; node cur,nxt;
for(int i = 0; i < 8; ++i) cur.a[i] = i+1;
cur.a[8] = 0; cur.loc = 8;
cur.cantor = 46234;//12356780 hash
q.push(cur);
while(!q.empty())
{
cur = q.front();
q.pop();
for(int i = 0; i < 4; ++i)
{
//转换成行列
int tx = cur.loc/3+dx[i];
int ty = cur.loc%3+dy[i];
int tz = tx*3 + ty; //一维数组中的位置
if(tz >= 0 && tx < 3 && ty >= 0 && ty < 3 && tx >= 0){
nxt = cur;
nxt.loc = tz;
nxt.a[cur.loc] = nxt.a[nxt.loc];
nxt.a[nxt.loc] = 0;
nxt.cantor = Cantor(nxt.a,9);//计算康托排列位置
if(!vis[nxt.cantor]){
vis[nxt.cantor] = true;
nxt.path.push_back(dir[i]);
q.push(nxt);
path[nxt.cantor] = nxt.path;
}
}
}
}
}
int main()
{
char x;
bfs();
while(~scanf(" %c",&x))
{
node cur;
if(x=='x') cur.a[0] = 0;
else cur.a[0] = x-'0';
for(int i = 1; i <= 8; ++i){
scanf(" %c",&x);
if(x=='x') cur.a[i] = 0;
else cur.a[i] = x-'0';
}
cur.cantor = Cantor(cur.a,9);
if(vis[cur.cantor]==0) printf("unsolvable");
else {
for(int i = path[cur.cantor].size()-1; i >= 0; --i)
printf("%c",path[cur.cantor][i]);
};
puts("");
}
return 0;
}