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
代码:<参考>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
using namespace std;
const int MAXN=1000000;//最多是9!/2
int fac[]={1,1,2,6,24,120,720,5040,40320,362880};//康拖展开判重
// 0!1!2!3! 4! 5! 6! 7! 8! 9!
bool vis[MAXN];//标记
string path[MAXN];//记录路径
int cantor(int s[])//康拖展开求该序列的hash值
{
int sum=0;
for(int i=0;i<9;i++)
{
int num=0;
for(int j=i+1;j<9;j++)
if(s[j]<s[i])
num++;
sum+=(num*fac[9-i-1]);
}
return sum+1;//0!为1.
}
struct Node
{
int s[9];
int loc;//表示“0”的位置
int status;//表示康拖展开的hash值
string path;//结果路径
};
int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//up,down,left,right.
char indexs[5]="durl";//和上面的要相反,因为是反向搜索,由目标状态求原始状态
int aim=46234;//123456780对应的康拖展开的hash值
void bfs()
{
memset(vis,false,sizeof(vis));//假设所有的康托展开没有访问过。
Node now,next;
for(int i=0;i<8;i++)//初始化为目标状态
now.s[i]=i+1;
now.s[8]=0;
now.loc=8;
now.status=aim;
now.path="";
queue<Node>q;//广度优先搜索用队列
q.push(now);//当前状态入队
path[aim]="";
while(!q.empty())
{
now=q.front();
q.pop();
int x=now.loc/3;//当前数字在方格中的位置。
int y=now.loc%3;
for(int i=0;i<4;i++)//
{
int tx=x+move[i][0];//数字在四个方向上移动后的坐标
int ty=y+move[i][1];
if(tx<0||tx>2||ty<0||ty>2)
continue;
next=now;
next.loc=tx*3+ty;//由坐标,求出现在该位置上的数字。
next.s[now.loc]=next.s[next.loc];
next.s[next.loc]=0;
next.status=cantor(next.s);
if(!vis[next.status])//如果该康托展开值没有出现过,即该排序序列没出现过,判断是否重复
{
vis[next.status]=true;//更新访问状态
next.path=indexs[i]+next.path;//一步路径
q.push(next);//现在状态入队
path[next.status]=next.path;//存放每步成功的路径
}
}
}
}
int main()
{
char ch;
Node cur;
bfs();
while(cin>>ch)
{
if(ch=='x') //把“x”看做“0”.
{
cur.s[0]=0;
cur.loc=0;
}
else cur.s[0]=ch-'0';
for(int i=1;i<9;i++)
{
cin>>ch;
if(ch=='x')
{
cur.s[i]=0;
cur.loc=i;
}
else cur.s[i]=ch-'0';
}
cur.status=cantor(cur.s);
if(vis[cur.status])
{
cout<<path[cur.status]<<endl;
}
else cout<<"unsolvable"<<endl;
}
return 0;
}
思路解析:
经典八码难题,康托展开判重 + BFS反向搜索。
康托展开:实质是计算当前排列在所有由小到大全排列中的顺序,因此是可逆的。
X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!
例如,3 5 7 4 1 2 9 6 8 展开为 98884。因为X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884.
解释:
排列的第一位是3,比3小的数有两个,以这样的数开始的排列有8!个,因此第一项为2*8!
排列的第二位是5,比5小的数有1、2、3、4,由于3已经出现,因此共有3个比5小的数,这样的排列有7!个,因此第二项为3*7!
以此类推,直至0*.当使用BFS算法时,首先要先检验上、下、左、右的邻居节点,
由于初始状态很多,目标状态只有一种,所以有目标状态反向求初始状态。
网上关于八数码的解题有很多,有人总结了八境界,膜拜了。。。
我个人只是认为这个方法,好理解一点,与大家共享。。。
还有代码的样例输出与结果不一样,估计是打印错误,代码可以AC掉的。。。