代码转自:http://blog.csdn.net/libin56842/article/details/23185751
Problem Description
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
题意:经典八数码问题,给定八数码的初始序列,求经过u、r、l、d四种操作到达1 2 3 4 5 6 7 8 x的状态,打印出操作序列?
思路:用a*的做法 每次都是进行估价 即f=g+h
其中也用到了康脱展开 a0*0!+a1*1!...a8*8!
因为康脱展开式的值对应唯一的状态 所以可以用它来判重
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
struct node
{
int f,h,g;//估价函数的三个值 f=g+h g为初始点该点的实际代价 h为该点到终点的顾及代价
int x,y; //x的坐标位置
char maps[5][5];
friend bool operator <(const node &a,const node &b)
{
if(a.f==b.f)return a.g<b.g;
return a.f>b.f;
}
}start;
char str[50];
int sum[15];//阶乘
int pos[][2]= {{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};//9个点的坐标
bool vis[500000];//用于判重
char ans[500000];//记录变化
int pre[500000];//记录康拓值
int to[4][2] = {1,0,0,1,-1,0,0,-1};
char to_c[10] = "drul";
bool check()//检查是否可行
{
int cnt=0;
int s[20];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
s[3*i+j]=start.maps[i][j];
if(s[3*i+j] == 'x')
continue;
for(int k=3*i+j-1;k>=0;k--)
{
if(s[k] == 'x')
continue;
if(s[3*i+j]<s[k])//统计每个点在他位置前有多少比他的数
cnt++;
}
}
}
if(cnt%2)//为奇数 不成立
return false;
else return true;
}
int solve(node a)//康托展开求值
{
int ans=0;
int s[20];//存每个位置的值
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
s[i*3+j]=a.maps[i][j];
int cnt=0;
for(int k=3*i+j-1; k>=0; k--)
{
if(s[k]>s[3*i+j])
cnt++;
}
ans = ans+sum[i*3+j]*cnt;
}
}
return ans;
}
int get_h(node a)//得到H值 当前的状态到目标状态0 1 2 ....8的估计代价
{
int i,j;
int ans = 0;
for(i = 0; i<3; i++)
{
for(j = 0; j<3; j++)
{
if(a.maps[i][j] == 'x')
continue;
int k = a.maps[i][j]-'1';
ans+=abs(pos[k][0]-i)+abs(pos[k][1]-j);
}
}
return ans;
}
void bfs()
{
memset(vis,false,sizeof vis);
priority_queue<node>q;
start.h=get_h(start);//从当前点到目标的预测代价
start.g=0;
start.f=start.g+start.h;
q.push(start);
while(!q.empty())
{
node a=q.top();
node next;
q.pop();
int k_s=solve(a);//求a的康脱展开式的值
for(int i=0;i<4;i++)
{
next=a;
next.x+=to[i][0];
next.y+=to[i][1];//x点的转移
if(next.x < 0 || next.y < 0 || next.x>2 || next.y > 2)
continue;
next.maps[a.x][a.y] = a.maps[next.x][next.y];//x点与其旁边的点的交换
next.maps[next.x][next.y] = 'x';
next.g++;//转化一次 初始当当前的代价为上一步加1
next.h=get_h(next);
next.f= next.h+ next.g;
int k_n = solve(next);
if(vis[k_n])//对康拓展开式的值 进行判重 每个康拓展开式的值对应唯一的状态
continue;
vis[k_n] = true;
q.push(next);
pre[k_n] = k_s;//记录康拓值 用于查找上一个状态
ans[k_n] = to_c[i];//记录如何变化的 用于结果的输出
if(k_n == 0)//如果到达目标状态
return ;
}
}
}
int main()
{
int x,y,len,i,j;
sum[0]=1;
for(int i=1;i<=9;i++)//阶乘
sum[i]=sum[i-1]*i;
while(gets(str))//读取一整行
{
x=y=0;
len=strlen(str);
for(i =0;i<len;i++)//初始化
{
if(str[i]>='0'&&str[i]<='9'||str[i]=='x')
{
start.maps[x][y]=str[i];
if(start.maps[x][y] == 'x')
{
start.x = x;
start.y = y;
}
y++;
if(y==3)
{
y = 0;
x++;
}
}
}
if(!check())//判断情况是否可能
{
printf("unsolvable\n");
continue;
}
int sa=solve(start);//求初始的康托展开的值
bfs();
stack<int> s;
int now = 0;
while(sa!=now)
{
s.push(ans[now]);
now = pre[now];//通过康脱展开的值不断查找
}
while(!s.empty())
{
putchar(s.top());
s.pop();
}
printf("\n");
}
return 0;
}