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
题意:典型的八数码问题,就是给了一个3*3的矩阵,求最少要对 “x” 经过怎样的移动能使矩阵变成 1 2 3 4 5 6 7 8 x。
思路:由于是多组数据,怕一般搜索会超时,因此我们可以倒着搜(从目标状态往其他状态搜,记录每种状态的反向路径),打一个表,当然这个表就要用map来存了,其中路径用string来记录。切记是倒着搜的,因此要记录的是反向路径,输出的时候要从后往前输出。因为是9个数,因此我们可以用int型来记录每一种状态。目标状态(123456780)
代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
map<int,string>v;
map<int,int>u;
struct node
{
string t;
int s[3][3]; //矩阵
int x,y;
};
void dfs()
{
queue<node>Q;
node p,q;
int i,j,k=1;
for(i=0;i<3;i++) //目标状态
for(j=0;j<3;j++)
q.s[i][j]=k++;
q.s[2][2]=0; //最后一个是x,此时用0来表示
q.x=2; //x的位置
q.y=2;
u[123456780]=1; //目标状态
Q.push(q);
while(!Q.empty())
{
p=Q.front();
Q.pop();
for(i=0;i<4;i++)
{
int x=p.x+dir[i][0];
int y=p.y+dir[i][1];
if(x<0||y<0||x>=3||y>=3)
continue;
for(j=0;j<3;j++)
for(k=0;k<3;k++)
q.s[j][k]=p.s[j][k];
swap(q.s[p.x][p.y],q.s[x][y]); //交换一下位置
int sum=1;
for(j=0;j<3;j++)
for(k=0;k<3;k++)
sum=sum*10+q.s[j][k]; //求此时的状态(int)
if(u[sum]) //出现过就不用入队列了
continue;
u[sum]=1;
q.x=x;
q.y=y;
if(i==0)
q.t=p.t+'l'; //反向存路径
else if(i==1)
q.t=p.t+'u';
else if(i==2)
q.t=p.t+'r';
else
q.t=p.t+'d';
Q.push(q);
v[sum]=q.t; //用map记录这个状态的路径
}
}
}
int main()
{
dfs();
char str[50];
while(gets(str))
{
int l=strlen(str);
int sum=1;
for(int i=0;i<l;i++)
{
if(str[i]>='1'&&str[i]<='9')
sum=sum*10+str[i]-'0';
else if(str[i]=='x')
sum=sum*10;
}
if(!u[sum]) //没有这种状态
{
printf("unsolvable\n");
continue;
}
string s=v[sum];
l=s.size();
for(int i=l-1;i>=0;i--) //逆序输出路径
printf("%c",s[i]);
printf("\n");
}
}