Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15216 Accepted Submission(s): 4235
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:
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:
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.
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
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
Source
/*
对于一个9的序列,变成3*3宫格,x表示可以移动的,求这样使他回到有序
123
456
780
思路:康托hash,然后path就录当前状态是由哪一个状态变来,
然后di[][]表示当前状态由前一个状态由哪一个方向变来
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define eps 1e-8
typedef __int64 ll;
#define bug printf("hi\n");
using namespace std;
#define INF 0x3f3f3f3f
#define N 400000
int fac[10];
int di[N],pre[N];
int vis[N];
struct stud{
int a[10];
}f[N];
int step[4][2]={1,0,-1,0,0,1,0,-1};
char c[]="udlr"; //因为要逆着走,所以方向相反
int start;
void inint()
{
int i,j;
fac[0]=1;
for(i=1;i<9;i++)
fac[i]=fac[i-1]*i;
}
int cantor(int a[])
{
int temp,i,j;
temp=0;
int cnt;
for(i=0;i<9;i++)
{
cnt=0;
for(j=i+1;j<9;j++)
if(a[i]>a[j]) cnt++;
temp+=cnt*fac[9-i-1];
}
return temp;
}
void bfs()
{
int i,j;
int head,tail;
stud cur,next;
for(i=0;i<8;i++)
cur.a[i]=i+1;
cur.a[8]=0;
start=cantor(cur.a);
memset(vis,0,sizeof(vis));
vis[start]=1;
head=tail=0;
f[tail++]=cur;
while(head<tail) //这里最好不要用queue,不然会超时
{
cur=f[head];
head++;
for(i=0;i<9;i++)
if(cur.a[i]==0) break;
int x=i/3;
int y=i%3;
for(i=0;i<4;i++)
{
int xx=x+step[i][0];
int yy=y+step[i][1];
int id=cantor(cur.a);
if(xx<0||xx>=3||yy<0||yy>=3) continue;
next=cur;
swap(next.a[x*3+y],next.a[xx*3+yy]);
int temp=cantor(next.a);
if(!vis[temp])
{
vis[temp]=1;
pre[temp]=id;
di[temp]=i;
f[tail++]=next;
}
swap(next.a[x*3+y],next.a[xx*3+yy]);
}
}
}
int main()
{
inint();
int i,j;
bfs();
int a[10];
char s[10];
while(~scanf("%1s",s))
{
if(s[0]=='x') a[0]=0;
else a[0]=s[0]-'0';
for(i=1;i<9;i++)
{
scanf("%1s",s);
if(s[0]=='x') a[i]=0;
else a[i]=s[0]-'0';
}
int temp=cantor(a);
if(!vis[temp])
printf("unsolvable\n");
else
{
while(temp!=start)
{
printf("%c",c[di[temp]]);
temp=pre[temp];
}
printf("\n");
}
}
return 0;
}