POJ1077 Eight —— A*算法
来源:互联网 发布:华腾软件 编辑:程序博客网 时间:2024/05/19 05:05
主页面:http://blog.csdn.net/dolfamingo/article/details/77825569
关于A*算法:g(n)表示从起点到任意节点n的路径花费,h(n)表示从节点n到目标节点路径花费的估计值(启发值),f(n) = g(n)+h(n)。
A*算法必须满足的条件(能不能满足由所选的h(n)估计方式决定):每次扩展的节点的 f 值 >= 父节点的 f 值小。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <set>#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 1e6+10;#define AIM 1 //123456789的哈希值为1struct node{ int status; int s[9]; int loc; int g,h,f; bool operator<(const node x)const{ return f>x.f; }};int vis[MAXN], fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};int dir[4][2] = { -1,0, 1,0, 0,-1, 0,1 };char op[4] = {'u', 'd', 'l', 'r' };char path[MAXN];int pre[MAXN];int cantor(int s[]) //获得哈希函数值{ int sum = 0; for(int i = 0; i<9; i++) { int num = 0; for(int j = i+1; j<9; j++) if(s[j]<s[i]) num++; sum += num*fac[8-i]; } return sum+1;}int dis_h(int s[]) //获得曼哈顿距离{ int dis = 0; for(int i = 0; i<9; i++) if(s[i]!=9) //‘x’不能算进去,否则不能满足:“每次扩展的节点的 f 值 >= 父节点的 f 值小” { int x = i/3, y = i%3; int xx = (s[i]-1)/3, yy = (s[i]-1)%3; dis += abs(x-xx) + abs(y-yy); } return dis;}priority_queue<node>que;bool Astar(node now){ ms(vis,0); while(!que.empty()) que.pop(); now.status = cantor(now.s); now.g = 0; now.h = dis_h(now.s); now.f = now.f + now.h; pre[now.status] = -1; //开始状态的上一个状态为-1,用于输出路径时“刹车” vis[now.status] = 1; que.push(now); node tmp; while(!que.empty()) { now = que.top(); que.pop(); if(now.status==AIM) //找到了123456789的状态 return true; int x = now.loc/3; int y = now.loc%3; for(int i = 0; i<4; i++) { int xx = x + dir[i][0]; int yy = y + dir[i][1]; if(xx>=0 && xx<=2 && yy>=0 && yy<=2) { tmp = now; tmp.s[x*3+y] = tmp.s[xx*3+yy]; //交换位置,下同 tmp.s[xx*3+yy] = 9; tmp.status = cantor(tmp.s); if(!vis[tmp.status]) { vis[tmp.status] = 1; tmp.loc = xx*3+yy; tmp.g++; //g tmp.h = dis_h(tmp.s); //h tmp.f = tmp.g + tmp.h; //f pre[tmp.status] = now.status; //tmp.status的上一个状态为now.status path[tmp.status] = op[i]; //保存操作 que.push(tmp); } } } } return 0;}void Print(int status){ if(pre[status]==-1) return; Print(pre[status]); //追溯上一个状态 putchar(path[status]);}int main(){ char str[50]; while(gets(str)) { node now; int cnt = 0; for(int i = 0; str[i]; i++) { if(str[i]==' ') continue; if(str[i]=='x') now.s[cnt] = 9, now.loc = cnt++; else now.s[cnt++] = str[i]-'0'; } if(!Astar(now)) puts("unsolvable"); else Print(AIM), putchar('\n'); }}
阅读全文
0 0
- POJ1077 Eight —— A*算法
- POJ1077 Eight —— IDA*算法
- POJ1077——Eight
- poj1077 Eight —— 正向bfs+康拓
- POJ1077 Eight —— 正向BFS
- POJ1077 Eight —— 反向BFS
- POJ1077 Eight —— 双向BFS
- POJ1077 Eight —— 经典的搜索问题
- 八数码 poj1077 Eight(A*、IDA*)
- 【POJ1077】Eight-A*+康托展开
- poj1077 Eight
- POJ1077、HDU1043 Eight 八数码问题:双向BFS、A*
- POJ1077 HDU1043 Eight 八数码 (A*+康托展开)
- poj1077 /hdoj 1043 Eight
- 【POJ1077】Eight【IDA*】
- Eight -- A*算法
- poj1077八数码问题——境界五(A*)
- Eight poj1077 广度优先搜索
- POJ1077 Eight —— 双向BFS
- hexo搭建个人博客
- 图像标记
- MongDB简单介绍
- SpringMVC下logback配置环境分离
- POJ1077 Eight —— A*算法
- HDU 6188 贪心
- Objective-c从零开始(一)
- Docker的简单介绍
- 在LaTeX中,使用argmin,argmax时,设置下标的方法
- kindeditor在方法中动态创建在线文本编辑器
- kuangbin 简单搜索 A 棋盘问题
- andriod解析XML文件,超级简单!
- C#中的委托,事件,回调函数综合总结:结合了网上几位大神的例子