Problem 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:
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
我是第一次做A*搜索 ,感觉好难啊,看了一天多了,表面的意思差不多明白了,但还没有深度的了解,以后慢慢再做这类题吧
是我的参考ACM之家上的题解,希望对你们有帮助
A*算法入门:http://www.cppblog.com/mythit/archive/2009/04/19/80492.aspx
本题详细解析请看:http://blog.csdn.net/acm_cxlove/article/details/7745323
第一个A*搜索,A*是一种启发式搜索,g为已花代价,h为估计的剩余代价,而A*是根据f=g+h作为估价函数进行排列,也就是优先选择可能最优的节点进行扩展。但是在搜索之前需要判断是否有解,而判断的方法就是根据逆序对数。
参考 代码
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#include <numeric>//常用数字操作 一般和algorithm搭配使用
#include <functional>//STL定义运算函数(代替运算符)
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn =1000008;
struct Node
{
int maze[3][3];
int h,g;//估计函数
int x,y;
int Hash;
bool operator< (const Node n1) const
{
return h!=n1.h? h>n1.h:g>n1.g;
}
bool check() //判断是否合法
{
if(x>=0&&x<3&&y>=0&&y<3)
return true ;
return false;
}
};
Node s,u,v,tt;
int HASH[9]= {1,1,2,6,24,120,720,5040,40320};
int destination=322560;
int vis[400000];
int pre[400000];
int way[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
//void debug(Node tmp)
//{
// for(int i=0; i<3; i++)
// {
// for(int j=0; j<3; j++)
// printf("%d ",tmp.maze[i][j]);
// printf("\n");
// }
// printf("%d %d\n%d %d\n",tmp.x,tmp.y,tmp.g,tmp.h);
// printf("hash=%d\n",tmp.Hash);
//}
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.maze[i][j];
int res=0;
for(int i=0; i<9; i++)
{
int k=0;
for(int j=0; j<i; j++)
if(a[j]>a[i]) k++;
res+=HASH[i]*k;
}
return res;
}
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.maze[i][j];
int sum=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]) sum++;
return !(sum&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.maze[i][j])
ans+=abs(i-(tmp.maze[i][j]-1)/3)+abs(j-(tmp.maze[i][j]-1)%3);
return ans;
}
void astar()
{
priority_queue<Node>que;
que.push(s);
while(!que.empty())
{
u=que.top();
que.pop();
for(int i=0; i<4; i++)
{
v=u;
v.x+=way[i][0];
v.y+=way[i][1];
if(v.check())
{
swap(v.maze[v.x][v.y],v.maze[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);
que.push(v);
}
if(v.Hash==destination) return;
}
}
}
}
void print()
{
string ans;
ans.clear();
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,i,j;
memset(vis,-1,sizeof(vis));
memset(pre,-1,sizeof(pre));
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if((str[k]<='9'&&str[k]>='0')||str[k]=='x')
{
if(str[k]=='x')
{
s.maze[i][j]=0;
s.x=i;
s.y=j;
}
else
s.maze[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);
astar();
print();
}
return 0;
}