八数码:
题目描述:
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 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
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
题目大意:
其实这道题很简单理解,就是简单的九宫格排序问题,而只能移动上下左右这四个方向,并且还要满足1 2 3 4 5 6 7 8 x这样的排序即可,所以这道题可以用BFS+康托正向定理去解就很快解了。
思路分析:
这道题有许多种解法,这里只讲一下BFS+康托定理
康托定理:
X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!,
其中a[i]为当前未出现的元素中是排在第几个(从0开始)。这就是康
托展开
1. 举个简单的例子吧,例如(1,2,3,4)这四种数字全排列,如果要
求2134的排在全排列第几个,可以这样算:
2. 首位是2,比他小的有1,那么就有1*(4-1)!。
3. 第二位是1,没有比他小,那么就有0*(4-2)!。
4. 第三位是3,比他小的有 1和2,但是1和2都已经出现在前面了,所以
0*(4-3)!
5. 第四位是4,比他小的有1和2和3,但是都出现在前面,所以有0*(0)!
6. 最后将其全部相加就是该数字在全排列中的排序。
代码:
#include<iostream>
#include<cstdio>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
using namespace std;
const int maxn=363000;//设置9的阶乘最大数
int fc[10]={1,1,2,6,24,120,720,5040,40320,362880};//阶乘
int di[5][2]={{-1,0},{0,1},{1,0},{0,-1}};//方向坐标
char di2[5]={"urdl"};//方向符号
int visit[maxn];//访问标记
int Hash(int s[]); //康托定理正向解密
typedef struct Lemon
{
int num;//Hash的值
int ans;//记录x的值
string s1;//记录操作方向符号
int s[10];//记录符号
}Lemon;
Lemon Frist;
int LemonBFS()
{
Lemon now,next;
queue<Lemon>q;
q.push(Frist);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.num==46234)//因为123456780的Hash值等于46234以此作为临界
{
cout<<now.s1;
return 1;
}
int k=now.ans;
int x1=k/3;
int y1=k%3;
//找出九宫格的坐标与方位
for(int i=0;i<4;i++)
{
int tx=x1+di[i][0];
int ty=y1+di[i][1];
if(tx<0 || ty<0 || tx>2 || ty>2)//边界条件判断
continue;
else
{
next=now;
next.ans=tx*3+ty;
next.s[now.ans]=next.s[next.ans];
next.s[next.ans]=0;
//交换2个数字之间的位置
next.num=Hash(next.s);
if(!visit[next.num])//看是否该Hash值被访问过
{
visit[next.num]=1;
next.s1=next.s1+di2[i];//添加di2字符
q.push(next);
}
}
}
}
return 0;
}
int Hash(int s[])//康托正向定理解码
{
int num=1;
for(int i=0;i<9;i++)
{
int temp=0;
for(int j=i+1;j<9;j++)
{
if(s[j]<s[i])
{
temp++;
}
}
num+=fc[9-i-1]*temp;
}
return num;
}
int main()
{
char ch;
for(int i=0;i<9;i++)
{
cin >> ch;
if(ch=='x')
{
Frist.s[i]=0;
Frist.ans=i;//记录x出现的位置
}
else
{
Frist.s[i]=ch-'0';
}
}
Frist.num=Hash(Frist.s);//记录该排序的Hash值
if(!LemonBFS())//搜索
cout<<"unsolvable"<<endl;
}