Eight
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
题目大意:给你一个状态让你变成初始状态12345678X,输出最少步数路径,如果不行输出unsolvable
思路:这就是经典的八数码问题,现在暂时还只会一种解法。就是BFS+康托展开的解法,首先由目标状态得到初始状态和由初始状态得到目标状态其实是一样的,只不过路径是逆向的。那么我们就可以直接通过打表记录所有由初始状态变换得到的状态的路径。但是我们没办法直接记录状态,没法开876543210这么大的数组,可以想到用Hash,但是这里有一种更好的方法就是康托展开。如果不知道的可以看:康托展开百度百科
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
using namespace std;
struct node
{
int a[9];
int hash,pos; //hash是现在的转态在全排列中的位置,pos是x所在的位置
string path; //状态a到初始状态的路径
};
int fac[10]={0,1,2,6,24,120,720,5040,40320,362880};
string path[400005];
int vis[400005];
char der[5]="lurd"; //这里方向要与实际相反,记录的是逆向路径
int go[4][2]={0,1,1,0,0,-1,-1,0};
int fun(int a[]) //康托展开,计算现在的排列方式在全排列中的位置
{
int sum=0;
for(int i=0;i<9;i++)
{
int t=0;
for(int j=i+1;j<9;j++)
if(a[i]>a[j])
t++;
sum+=t*fac[9-i-1];
}
return sum;
}
void init() //BFS 打表所有状态到初始状态的路径
{
memset(vis,0,sizeof(vis));
node no,ne;
for(int i=0;i<8;i++) no.a[i]=i+1; //初始状态
no.a[8]=0;
no.hash=fun(no.a);
no.pos=8;
no.path="";
path[no.hash]="";
queue<node>q;
vis[no.hash]=1;
q.push(no);
while(!q.empty())
{
no=q.front();
q.pop();
int x=no.pos/3,y=no.pos%3;
for(int i=0;i<4;i++) //x可以向上下左右四个方向移动
{
int xx=x+go[i][0],yy=y+go[i][1];
if(xx>=0&&yy>=0&&xx<3&&yy<3)
{
ne=no;
ne.pos=xx*3+yy;
ne.a[no.pos]=no.a[ne.pos];
ne.a[ne.pos]=0;
ne.hash=fun(ne.a);
if(!vis[ne.hash]) //如果这个状态之前没出现过
{
vis[ne.hash]=1;
ne.path=der[i]+no.path; //记录逆向路径
path[ne.hash]=ne.path;
q.push(ne);
}
}
}
}
}
int main()
{
init();
node ans;
char s;
while(~scanf(" %c",&s))
{
if(s=='x') ans.a[0]=0;
else ans.a[0]=s-'0';
for(int i=1;i<9;i++)
{
scanf(" %c",&s);
if(s=='x') ans.a[i]=0;
else ans.a[i]=s-'0';
}
ans.hash=fun(ans.a);
if(vis[ans.hash])
cout<<path[ans.hash]<<endl;
else printf("unsolvable\n");
}
return 0;
}