POJ1077 Eight —— A*算法
主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html
关于A*算法:g(n)表示从起点到任意节点n的路径花费,h(n)表示从节点n到目标节点路径花费的估计值(启发值),f(n) = g(n)+h(n)。
A*算法必须满足的条件(能不能满足由所选的h(n)估计方式决定):每次扩展的节点的 f 值 >= 父节点的 f 值。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const int INF = 2e9; 16 const LL LNF = 9e18; 17 const int MOD = 1e9+7; 18 const int MAXN = 1e6+10; 19 #define AIM 1 //123456789的哈希值为1 20 21 struct node 22 { 23 int status; 24 int s[9]; 25 int loc; 26 int g,h,f; 27 bool operator<(const node x)const{ 28 return f>x.f; 29 } 30 }; 31 32 int vis[MAXN], fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320}; 33 int dir[4][2] = { -1,0, 1,0, 0,-1, 0,1 }; 34 char op[4] = {'u', 'd', 'l', 'r' }; 35 char path[MAXN]; 36 int pre[MAXN]; 37 38 int cantor(int s[]) //获得哈希函数值 39 { 40 int sum = 0; 41 for(int i = 0; i<9; i++) 42 { 43 int num = 0; 44 for(int j = i+1; j<9; j++) 45 if(s[j]<s[i]) 46 num++; 47 sum += num*fac[8-i]; 48 } 49 return sum+1; 50 } 51 52 int dis_h(int s[]) //获得曼哈顿距离 53 { 54 int dis = 0; 55 for(int i = 0; i<9; i++) 56 if(s[i]!=9) //‘x’不能算进去,否则不能满足:“每次扩展的节点的 f 值 >= 父节点的 f 值小” 57 { 58 int x = i/3, y = i%3; 59 int xx = (s[i]-1)/3, yy = (s[i]-1)%3; 60 dis += abs(x-xx) + abs(y-yy); 61 } 62 return dis; 63 } 64 65 priority_queue<node>que; 66 bool Astar(node now) 67 { 68 ms(vis,0); 69 while(!que.empty()) que.pop(); 70 71 now.status = cantor(now.s); 72 now.g = 0; 73 now.h = dis_h(now.s); 74 now.f = now.f + now.h; 75 pre[now.status] = -1; //开始状态的上一个状态为-1,用于输出路径时“刹车” 76 vis[now.status] = 1; 77 que.push(now); 78 79 node tmp; 80 while(!que.empty()) 81 { 82 now = que.top(); 83 que.pop(); 84 if(now.status==AIM) //找到了123456789的状态 85 return true; 86 87 int x = now.loc/3; 88 int y = now.loc%3; 89 for(int i = 0; i<4; i++) 90 { 91 int xx = x + dir[i][0]; 92 int yy = y + dir[i][1]; 93 if(xx>=0 && xx<=2 && yy>=0 && yy<=2) 94 { 95 tmp = now; 96 tmp.s[x*3+y] = tmp.s[xx*3+yy]; //交换位置,下同 97 tmp.s[xx*3+yy] = 9; 98 tmp.status = cantor(tmp.s); 99 if(!vis[tmp.status]) 100 { 101 vis[tmp.status] = 1; 102 tmp.loc = xx*3+yy; 103 tmp.g++; //g 104 tmp.h = dis_h(tmp.s); //h 105 tmp.f = tmp.g + tmp.h; //f 106 pre[tmp.status] = now.status; //tmp.status的上一个状态为now.status 107 path[tmp.status] = op[i]; //保存操作 108 que.push(tmp); 109 } 110 } 111 } 112 } 113 return 0; 114 } 115 116 void Print(int status) 117 { 118 if(pre[status]==-1) return; 119 Print(pre[status]); //追溯上一个状态 120 putchar(path[status]); 121 } 122 123 int main() 124 { 125 char str[50]; 126 while(gets(str)) 127 { 128 node now; 129 int cnt = 0; 130 for(int i = 0; str[i]; i++) 131 { 132 if(str[i]==' ') continue; 133 if(str[i]=='x') now.s[cnt] = 9, now.loc = cnt++; 134 else now.s[cnt++] = str[i]-'0'; 135 } 136 if(!Astar(now)) 137 puts("unsolvable"); 138 else 139 Print(AIM), putchar('\n'); 140 } 141 }