Eight
----------------------------------------------------------------------------------------------------------------------------
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 x
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.
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
1 2 3 x 4 6 7 5 8
1 2 3 x 4 6 7 5 8Output
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
----------------------------------------------------------------------------------------------------
深夜写此篇的目的,是想告诉自己不要轻易放弃,肝了一天半。
八数码问题,拿到题之后由于求最短方案因此可以用bfs,难点在于如何保存每一个的状态。学长告诉我用康托展开,没听过,于是上链接
大神写的康托展开详解
值得注意的有以下几点:
1. 输入的时候用字符串输入,由于题目数据输入时有空格,所以需要预处理
2. 对于每个节点可以将地图的状态和康托展开值都保存进去,这样避免了逆康托展开
3. 和bfs一样,遍历过的点用v来保存,注意对初始值的处理
4. 输出路径是一个小技巧,这里用两个数组来实现(第一次用字符串复制的方法结果tle)
*5.用数学可以证明3*3的码交换时不改变排列的逆序数的奇偶性,所以可以先判断初末状态的逆序数来判断该问题是否有解
ac代码如下:
#include
#include
#include
#include
//此题逆向操作,可以在输出的时候少一次逆向输出
using namespace std;
int v[370000];//用于记录展开值状态是否出现过
int pre[370000];//用于保存改状态的上一个状态
char curr[370000];//用于保存改状态的移动路径
char dir[5]="lrud";//移动路径
int step;//记录最短的步数
int ed;//记录最后一次的状态
char st[10]="123456789";//用于存放初始状态(在题中是结束状态)
//声明了一个节点
struct node
{
int x,y;
int cantor;//保存康托展开值
int step;//保存步数
int map[3][3];//保存地图
};
//阶乘
int fac[]= {1,1,2,6,24,120,720,5040,40320,362880};//阶乘
int dx[4]= {0,0,1,-1};
int dy[4]= {1,-1,0,0};//由于是逆向操作,所以方向相反
//康托展开
int cantor(char arr[])
{
int i,j,sum1=0,sum2=0;
for(i=0; i<9; i++)
{
sum2=0;
for(j=i+1; j<9; j++)
{
if(arr[j] q;
node beg;
beg.x=2,beg.y=2,beg.cantor=cantor(st),beg.step=0;
int s,t,num=1;
for(s=0;s<3;s++)//对地图的初始化
{
for(t=0;t<3;t++)
{
beg.map[s][t]=num++;
}
}
q.push(beg);
while(!q.empty())
{
node cur=q.front();
q.pop();
int i;
for(i=0; i<4; i++)
{
node nxt;
nxt.x=cur.x+dx[i];
nxt.y=cur.y+dy[i];
if(nxt.x<0||nxt.x>2||nxt.y<0||nxt.y>2)
{
continue;
}
char crr[10];
int j,k,m=0;
for(j=0; j<3; j++)//将上一个状态的地图复制下来
{
for(k=0; k<3; k++)
{
nxt.map[j][k]=cur.map[j][k];
}
}
swap(nxt.map[cur.x][cur.y],nxt.map[nxt.x][nxt.y]);//将现在这个状态的地图做改变
for(j=0; j<3; j++)//将地图变为字符串
{
for(k=0; k<3; k++)
{
crr[m++]=nxt.map[j][k]+'0';
}
}
nxt.cantor=cantor(crr);
if(v[nxt.cantor]==1)//如果出现过,则返回循环
{
continue;
}
v[nxt.cantor]=1;
nxt.step=cur.step+1;
pre[nxt.cantor]=cur.cantor;//记录上一个状态
curr[nxt.cantor]=dir[i];//记录这一个状态的路径
if(nxt.cantor==ans)//如果找到了末状态,则
{
int a;
ed=nxt.cantor;
for(a=0;a='1'&&brr[i]<='8')
{
arr[j]=brr[i];
drr[j]=arr[j]-'0';
j++;
}
else if(brr[i]=='x')
{
arr[j]='9';
drr[j]=9;
j++;
}
}
int sum=0;
for(i=8; i>=0; i--)//记录初状态的逆序数
{
for(j=0; jdrr[i]&&drr[j]!=9&&drr[i]!=9)
{
sum++;
}
}
}
if(sum%2==1)
{
printf("unsolvable\n");
}
else
{
memset(v,0,sizeof(v));//初始化v
v[cantor(st)]=1;
ans=cantor(arr);
bfs();
}
return 0;
}