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.
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
2 3 4 1 5 x 7 6 8
ullddrurdllurdruldr
经典的八数码问题,类似于我们平时玩的拼图。在解题过程中遇到了许多问题,参考了别人的做法,总结整理一下。
首先对于一个图的状态,我们要将其对应为一个status,通过康托展开,将其映射到hash空间。 康托展开利用了从高位到低位,每一位中比其小并且在高位中没有出现过的数字的个数,乘以该数位的阶乘。(我们可以通过这种方式得到比他小的数有多少个)。
把每一种情况hash映射以后,我们就要对当前状态到目标状态进行搜索,为了使路径尽可能短并且搜索更高效,首先想到了bfs,即从给定的状态,每次将x移动一步进行广度搜索,结果一定是TLE
由于测试是special judge而且是多组数据,想到离线bfs,即从终点开始,反向搜索,找出所有的可能解,然后根据给定状态是否被访问,直接输出答案,结果内存超限,bfs的层数太多了。
同时通过参考得知,无解的情况是可以直接判断来剪枝的,由于目标街12345678x的逆序数是0,其中x看做空位,每次变换就是x和某一位数交换,由于是空位交换,可以看做两次普通的数字交换,我们知道一次任意数字变换必定改变序列的逆序奇偶性,所以x和数字交换一定不改变奇偶性,所以可以直接判断逆序奇偶性来判断是否有解。
最后采用A*的方法,其中f = g + h g用起始到当前的步数决定,h由当前状态到目标状态的曼哈顿距离表示。在搜索过程中,路径以记录上一节点位置和移动方向来记录,同时得到的结果是反向的,反向输出结果即可。
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1000000;//最多是9!
int fac[]={1,1,2,6,24,120,720,5040,40320,362880};//康拖展开判重
bool vis[MAXN];//标记
char path[MAXN];
int pre[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;
}
struct Node
{
int s[9];
int f, g, h; //存储函数
int loc;//“0”的位置
int status;//康拖展开的hash值
friend bool operator< (const Node &a,const Node &b)
{
if(a.f==b.f)
return a.g<b.g;
return a.f>b.f;
}
};
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//u,d,l,r
char indexs[5]="udlr";
int aim=46234;//123456780对应的康拖展开的hash值
int cheak(int s[])
{
int cnt = 0;
for(int i = 0; i < 9; ++i)
{
for(int j = i+1; j < 9; ++j)
if(s[j]<s[i] && s[j] && s[i]) cnt++;
}
if(cnt%2)
return 0;
return 1;
}
int get_h(const Node &x)
{
int ans=0;
for(int i=0;i<9;i++)
{
if(x.s[i] == 0) continue;
ans += abs(i/3-(x.s[i]-1)/3)+abs(i%3-(x.s[i]-1)%3);
}
//cout << ans << endl;
return ans;
}
void bfs(Node &cur)
{
//cout << "ok" << endl;
memset(vis,false,sizeof(vis));
Node next;
cur.g = 0;
cur.h = get_h(cur);
cur.f = cur.g + cur.h;
priority_queue<Node> q;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
int x=cur.loc/3;
int y=cur.loc%3;
for(int i=0;i<4;i++)
{
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx<0||tx>2||ty<0||ty>2)continue;
next=cur;
next.loc=tx*3+ty;
next.s[cur.loc]=next.s[next.loc];
next.s[next.loc]=0;
next.status=cantor(next.s);
next.g++;
next.h = get_h(next);
next.f = next.g + next.h;
if(!vis[next.status])
{
vis[next.status]=true;
pre[next.status] = cur.status;
path[next.status]= indexs[i];
q.push(next);
if(next.status == aim)
return;
}
}
}
}
int main()
{
char ch;
Node cur;
while(cin>>ch)
{
if(ch=='x') {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';
}
if(!cheak(cur.s))
{
cout << "unsolvable" << endl;
continue;
}
cur.status=cantor(cur.s);
int start = cur.status;
bfs(cur);
int now = aim;
stack<char> st;
while(start != now)
{
st.push(path[now]);
now = pre[now];
}
while(!st.empty())
{
cout << st.top();
st.pop();
}
cout << endl;
}
return 0;
}