POJ 1077 Eight 八数码 A*搜索算法

Eight
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12974   Accepted: 5788   Special Judge

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 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

Source

South Central USA 1998
 
八数码 绝对经典的题目。
可以用双向BFS,IDA*,A*算法。
 
A*算法简介:
A*(A-Star) 算法是一种静态路网中求解最短路最有
   POJ 1077 Eight 八数码 A*搜索算法_第1张图片

A star算法在静态路网中的应用

效的方法。   公式表示为: f(n)=g(n)+h(n),   其中f(n) 是从初始点经由节点n到目标点的估价函数,   g(n) 是在状态空间中从初始节点到n节点的实际代价,   h(n)是从n到目标节点最佳路径的估计代价。   保证找到 最短路径(最优解的)条件,关键在于估价函数h(n)的选取:   估价值h(n)<= n到目标节点的距离实际值,这种情况下,搜索的点数多,搜索范围大,效率低。但能得到最优解。   如果 估价值>实际值, 搜索的点数少,搜索范围小,效率高,但不能保证得到最优解。
 
 
 
详见百度百科
 
A*算法理论与实践
 
A*算法
http://blog.myspace.cn/e/404763245.htm
初识A*算法
深入A*算法
 我刚开始使用的hash函数:
int Hash(int *num) { int i=0,h=0; while(i<9) h=(h*13+num[i++])%362885; return h; }
后面看到网上写的变进制hash: 一种变进制数及其应用(全排列之Hash实现)
int b[9]={1,1,2,6,24,120,720,5040,40320}; int Hash(int *num) { int i,j,k,h=0; for(i=0;i<9;i++) { for(k=0,j=i+1;j<9;j++) if(num[i]>num[j]) k++; h+=k*b[num[i]]; } return h; }
判断逆序数奇偶性:
int fun(int *a) { int i,j,s=0; for(i=0;i<9;i++) for(j=i+1;j<9;j++) if(a[j]&&a[i]>a[j]) s++; return s; }
g(n):已经移动的步数
计算h(n):当前位置与目标位置的曼哈顿距离
int cal(int *num) { int i,s=0; for(i=0;i<9;i++) s+=abs((num[i]+8)%9%3-i%3)+abs((num[i]+8)%9/3-i/3); return s; }
 
全部代码:
//错误的代码在POJ也能AC。。。 #include<cstdio> #include<cstdlib> #include<cstring> #include<queue> #include<algorithm> using namespace std; int hash[362885],pre[362885],b[9]={1,1,2,6,24,120,720,5040,40320}; char d[6]="0lrud"; struct node { int p,f,g,h;//0的位置,父节点 int num[9]; node(int pp,int ff,int gg,int hh,int *a){p=pp;f=ff;g=gg;h=hh;for(int i=0;i<9;i++)num[i]=a[i];} bool operator<(const node x)const {return g+h>x.g+x.h;} }; int fun(int *a) { int i,j,s=0; for(i=0;i<9;i++) for(j=i+1;j<9;j++) if(a[j]&&a[i]>a[j]) s++; return s; } int cal(int *num) { int i,s=0; for(i=0;i<9;i++) s+=abs((num[i]+8)%9%3-i%3)+abs((num[i]+8)%9/3-i/3); return s; } int Hash(int *num) { int i,j,k,h=0; for(i=0;i<9;i++) { for(k=0,j=i+1;j<9;j++) if(num[i]>num[j]) k++; h+=k*b[num[i]]; } return h; } int A_star(node st) { priority_queue<node> q; int p,h,f; int num[9]; q.push(st); while(q.size()) { node x=q.top(); q.pop(); memcpy(num,x.num,sizeof(num)); p=x.p; f=Hash(num); if(cal(num)==0) return f; if(p%3!=0) { swap(num[p],num[p-1]); if(!hash[h=Hash(num)]) { hash[h]=1; pre[h]=f; q.push(node(p-1,f,x.g+1,cal(num),num)); } swap(num[p],num[p-1]); } if(p%3!=2) { swap(num[p],num[p+1]); if(!hash[h=Hash(num)]) { hash[h]=2; pre[h]=f; q.push(node(p+1,f,x.g+1,cal(num),num)); } swap(num[p],num[p+1]); } if(p/3!=0) { swap(num[p],num[p-3]); if(!hash[h=Hash(num)]) { hash[h]=3; pre[h]=f; q.push(node(p-3,f,x.g+1,cal(num),num)); } swap(num[p],num[p-3]); } if(p/3!=2) { swap(num[p],num[p+3]); if(!hash[h=Hash(num)]) { hash[h]=4; pre[h]=f; q.push(node(p+3,f,x.g+1,cal(num),num)); } swap(num[p],num[p+3]); } } return -1; } void dfs(int x) { if(pre[pre[x]]!=-1) dfs(pre[x]); printf("%c",d[hash[x]]); } int main() { int num[9],i,j,k; char c; for(i=0;i<9;i++) { while((c=getchar())==' '); if(c=='x') num[k=i]=0; else num[i]=c-'0'; } if((fun(num)&1)) puts("unsolvable"); else { hash[j=Hash(num)]=1; pre[j]=-1; i=A_star(node(k,0,0,cal(num),num)); if(i!=-1) dfs(i); else puts("unsolvable"); } }

你可能感兴趣的:(算法,String,each,Exchange,fun,Numbers)