Eight
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 29605 | Accepted: 12877 | Special Judge |
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 a description of a configuration of the 8 puzzle. The 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
is described by this list:
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.
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
Source
著名的八数码的问题;
我是采用A*算法解决的;
以各个点到应该所在位置的曼哈顿距离作为启发函数;
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<cmath>
#include<vector>
#include<stack>
using namespace std;
int maze[4][4];
bool vis[362885];
char prec[362885];
int precode[362885];
struct State
{
int status[9];
int h;
int g;
int x,y;
bool operator < (const State &a) const
{
return h+g>a.h+a.g;
}
State(const State& a)
{
for(int i=0;i<9;++i)
{
status[i]=a.status[i];
}
h=a.h;
g=a.g;
x=a.x;
y=a.y;
}
State(){}
};
State start;
int get_h(int status[])
{
int sum=0;
for(int i=0;i<9;++i)
{
if(status[i]==1)
sum+=abs(i/3)+abs(i%3);
if(status[i]==2)
sum+=abs(i/3)+abs(i%3-1);
if(status[i]==3)
sum+=abs(i/3)+abs(i%3-2);
if(status[i]==4)
sum+=abs(i/3-1)+abs(i%3);
if(status[i]==5)
sum+=abs(i/3-1)+abs(i%3-1);
if(status[i]==6)
sum+=abs(i/3-1)+abs(i%3-2);
if(status[i]==7)
sum+=abs(i/3-2)+abs(i%3);
if(status[i]==8)
sum+=abs(i/3-2)+abs(i%3-1);
}
return sum;
}
const int fac[]={1,1,2,6,24,120,720,5040,40320};
int KT(int s[],int n)
{
int i,j,cnt,sum;
sum=0;
for(i=0;i<n;++i)
{
cnt=0;
for(j=i+1;j<n;++j)
if(s[j]<s[i]) ++cnt;
sum+=cnt*fac[n-i-1];
}
return sum;
}
void left(int status[],int x,int y)
{
status[3*x+y]=status[3*x+y-1];
status[3*x+y-1]=0;
}
void right(int status[],int x,int y)
{
status[3*x+y]=status[3*x+y+1];
status[3*x+y+1]=0;
}
void up(int status[],int x,int y)
{
status[3*x+y]=status[3*(x-1)+y];
status[3*(x-1)+y]=0;
}
void down(int status[],int x,int y)
{
status[3*x+y]=status[3*(x+1)+y];
status[3*(x+1)+y]=0;
}
bool flag;
void bfs()
{
priority_queue<State> pq;
pq.push(start);
int code=KT(start.status,9);
//cout<<code<<endl;
vis[code]=1;
while(!pq.empty())
{
State tmp(pq.top());
pq.pop();
if(tmp.h==0)
{
flag=1;
return;
}
int tx=tmp.x,ty=tmp.y;
if(tx>0)
{
State temp(tmp);
up(temp.status,temp.x,temp.y);
code=KT(temp.status,9);
if(!vis[code])
{
vis[code]=1;
prec[code]='u';
precode[code]=KT(tmp.status,9);
temp.h=get_h(temp.status);
temp.g=tmp.g+1;
--temp.x;
pq.push(temp);
}
}
if(tx<2)
{
State temp(tmp);
down(temp.status,temp.x,temp.y);
code=KT(temp.status,9);
if(!vis[code])
{
vis[code]=1;
prec[code]='d';
precode[code]=KT(tmp.status,9);
temp.h=get_h(temp.status);
temp.g=tmp.g+1;
++temp.x;
pq.push(temp);
}
}
if(ty>0)
{
State temp(tmp);
left(temp.status,temp.x,temp.y);
code=KT(temp.status,9);
if(!vis[code])
{
vis[code]=1;
prec[code]='l';
precode[code]=KT(tmp.status,9);
temp.h=get_h(temp.status);
temp.g=tmp.g+1;
--temp.y;
pq.push(temp);
}
}
if(ty<2)
{
State temp(tmp);
right(temp.status,temp.x,temp.y);
code=KT(temp.status,9);
if(!vis[code])
{
vis[code]=1;
prec[code]='r';
precode[code]=KT(tmp.status,9);
temp.h=get_h(temp.status);
temp.g=tmp.g+1;
++temp.y;
pq.push(temp);
}
}
}
}
int main()
{
char c;
for(int i=0;i<9;++i)
{
scanf(" %c",&c);
if(c>='1'&&c<='8')
{
start.status[i]=c-'0';
}
else if(c=='x')
{
start.status[i]=0;
start.x=i/3;
start.y=i%3;
}
}
memset(vis,0,sizeof(vis));
start.h=get_h(start.status);
start.g=0;
flag=0;
memset(prec,0,sizeof(prec));
memset(precode,0,sizeof(precode));
bfs();
if(!flag)
printf("unsolvable\n");
else
{
stack<char> st;
while(!st.empty())
{
st.pop();
}
int a[]={1,2,3,4,5,6,7,8,0};
int code=KT(a,9);
//cout<<precode[code]<<endl;
while(code!=0)
{
st.push(prec[code]);
code=precode[code];
}
st.pop();
while(!st.empty())
{
printf("%c",st.top());
st.pop();
}
printf("\n");
}
return 0;
}