思路:八数码问题首先要会康托展开来表示它的状态, 根据逆序数直接判断有无解,对于一个八数码,依次排列之后,每次是将空位和相邻位进行调换,研究后会发现,每次调换,逆序数增幅都为偶数,也就是不改变奇偶性,所以只需要根据初始和目标状态的逆序数正负判断即可。 估价函数H:是根据与目标解的曼哈顿距离,也就是每个数字与目标位置的曼哈顿距离之和。
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int mp[3][3];
int h,g;
int Hash;
int x,y;
bool operator < (const Node a)const
{
if(h==a.h)
return g>a.g;
return h>a.h;
}
bool check()
{
if(x>=0 && x<3 && y>=0 && y<3)
return true;
return false;
}
}s,t;
int Hash[9]={1,1,2,6,24,120,720,5040,40320};
int destination = 322560;
int vis[400000];
int pre[400000];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int get_hash(Node tmp)
{
int a[9],k=0;
for(int i = 0;i<3;i++)
for(int j = 0;j<3;j++)
a[k++]=tmp.mp[i][j];
int ans = 0;
for(int i = 0;i<9;i++)
{
int k = 0;
for(int j = 0;j<i;j++)
if(a[j]>a[i])
k++;
ans+=Hash[i]*k;
}
return ans;
}
bool isok(Node tmp)
{
int a[9],k=0;
for(int i = 0;i<3;i++)
for(int j = 0;j<3;j++)
a[k++]=tmp.mp[i][j];
int ans = 0;
for(int i = 0;i<9;i++)
{
for(int j = i+1;j<9;j++)
if(a[j]&&a[i]&&a[i]>a[j])
ans++;
}
return !(ans&1);
}
int get_h(Node tmp)
{
int ans = 0;
for(int i = 0;i<3;i++)
for(int j = 0;j<3;j++)
if(tmp.mp[i][j])
ans+=abs(i-(tmp.mp[i][j]-1)/3)+abs(j-(tmp.mp[i][j]-1)%3);
return ans;
}
void bfs()
{
priority_queue<Node>q;
q.push(s);
while(!q.empty())
{
Node u = q.top();
q.pop();
for(int i = 0;i<4;i++)
{
Node v = u;
v.x+=dir[i][0];
v.y+=dir[i][1];
if(v.check())
{
swap(v.mp[v.x][v.y],v.mp[u.x][u.y]);
v.Hash = get_hash(v);
if(vis[v.Hash]==-1 && isok(v))
{
vis[v.Hash]=i;
v.g++;
pre[v.Hash]=u.Hash;
v.h=get_h(v);
q.push(v);
}
if(v.Hash==destination)
return;
}
}
}
}
void print()
{
string ans = "";
int nxt = destination;
while(pre[nxt]!=-1)
{
switch(vis[nxt])
{
case 0:ans+='r';break;
case 1:ans+='l';break;
case 2:ans+='d';break;
case 3:ans+='u';break;
}
nxt = pre[nxt];
}
for(int i = ans.size()-1;i>=0;i--)
putchar(ans[i]);
puts("");
}
int main()
{
char str[100];
while(gets(str)!=NULL)
{
int k = 0;
memset(vis,-1,sizeof(vis));
memset(pre,-1,sizeof(pre));
for(int i = 0;i<3;i++)
for(int j = 0;j<3;j++)
{
if((str[k]<='9' && str[k]>='0') || str[k]=='x')
{
if(str[k]=='x')
{
s.mp[i][j]=0;
s.x=i;
s.y=j;
}
else
s.mp[i][j]=str[k]-'0';
}
else
j--;
k++;
}
if(!isok(s))
{
printf("unsolvable\n");
continue;
}
s.Hash = get_hash(s);
if(s.Hash == destination)
{
puts("");
continue;
}
vis[s.Hash]=-2;
s.g=0;
s.h=get_h(s);
bfs();
print();
}
}
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
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