Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28030 Accepted Submission(s): 7456
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
第一次写Astar,也不是什么太神秘的东西,这代码写得真棒,简洁、高效、beauty.以后还好越来越棒
AC代码:
#include<iostream>
#include<queue>
#include<math.h>
#include<memory.h>
using namespace std;
int factory[]={1,1,2,6,24,120,720,5040,40320,362880};
int dir[4]={-1,-3,3,1};
int is_inverse_odd(char *map)
{
int cnt=0;
for(int i=0;i<9;i++)
for(int j=i+1;j<9;j++)
if(map[i]!='x'&&map[j]!='x'&&map[i]>map[j]) cnt++;
return cnt&1;
}
int get_id(char *map)
{
int id=0;
for(int i=0;i<9;i++)
{
int cnt=0;
for(int j=i+1;j<9;j++)
if(map[i]>map[j]) cnt++;
id+=cnt*factory[8-i];
}
return id+1;
}
int get_len(char *map)
{
int len=0;
for(int i=0;i<9;i++)
{
int pos=map[i]=='x'?8:map[i]-'0'-1;
len+=fabs(i/3-pos/3)+fabs(i%3-pos%3);
}
return len;
}
struct node
{
char map[9];
int id,step,min_len,pos;
node(char *c,int st)
{
for(int i=0;i<9;i++)
{
map[i]=c[i];
if(c[i]=='x') pos=i;
}
id=get_id(c);
step=st;
min_len=get_len(c);
}
bool operator <(const node &t)const
{
//return step+min_len>t.step+t.min_len;//998ms
return min_len>t.min_len||(min_len==t.min_len&&step>t.step);//312ms
}
};
int pre[362881],vis[362881];
void A_star(char *s)
{
memset(vis,0,sizeof(vis));
priority_queue<node> q;
node S=node(s,0);q.push(S);vis[S.id]=1;
while(!q.empty())
{
node tmp=q.top();q.pop();
if(tmp.min_len==0) break;
for(int i=0;i<4;i++)
{
int p=tmp.pos+dir[i];
if(p>=0&&p<9&&(p%3==tmp.pos%3||p/3==tmp.pos/3))
{
tmp.map[tmp.pos]=tmp.map[p];tmp.map[p]='x';
int id=get_id(tmp.map);
if(!vis[id])
{
vis[id]=1;
pre[id]=3-i;
q.push(node(tmp.map,tmp.step+1));
}
tmp.map[p]=tmp.map[tmp.pos];tmp.map[tmp.pos]='x';
}
}
}
return;
}
void print_path(char *map,char *tar,int pos)//典型递归模式输出path
{
int flag=1;
for(int i=0;i<9;i++)
if(map[i]!=tar[i])
flag=0;
if(flag) return;
int id=get_id(tar);
int p=pos+dir[pre[id]];
tar[pos]=tar[p];tar[p]='x';
print_path(map,tar,p);
switch(dir[3-pre[id]])
{
case -1:cout<<'l';break;
case -3:cout<<'u';break;
case 1:cout<<'r';break;
case 3:cout<<'d';break;
}
return;
}
char map[9];
int main()
{
//freopen("in.txt","r",stdin);
while(cin>>map[0])
{
for(int i=1;i<9;i++) cin>>map[i];
if(is_inverse_odd(map)) cout<<"unsolvable"<<endl;
else
{
A_star(map);char tar[9]={'1','2','3','4','5','6','7','8','x'};
print_path(map,tar,8);
cout<<endl;
}
}
return 0;
}