POJ1077 Eight —— IDA*算法
来源:互联网 发布:淘宝客服售后绩效模板 编辑:程序博客网 时间:2024/05/19 03:23
主页面:http://blog.csdn.net/dolfamingo/article/details/77825569
代码一:像BFS那样,把棋盘数组放在结构体中。
#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 s[9]; int loc;};int 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' };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) { 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;}char path[100];bool IDAstar(node now, int depth, int pre, int limit){ if(dis_h(now.s)==0) //搜到123456789 { path[depth] = '\0'; puts(path); return true; } int x = now.loc/3; int y = now.loc%3; for(int i = 0; i<4; i++) { if(i+pre==1 || i+pre==5) continue; //方向与上一步相反, 剪枝 int xx = x + dir[i][0]; int yy = y + dir[i][1]; if(xx>=0 && xx<=2 && yy>=0 && yy<=2) { node tmp = now; tmp.s[x*3+y] = tmp.s[xx*3+yy]; tmp.s[xx*3+yy] = 9; tmp.loc = xx*3+yy; path[depth] = op[i]; if(depth+1+dis_h(tmp.s)<=limit) //在限制内 if(IDAstar(tmp, depth+1, i, limit)) return true; } } return false;}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'; } int limit; for(limit = 0; limit<=100; limit++) //迭代加深搜 if(IDAstar(now, 0, INF, limit)) break; if(limit>100) puts("unsolvable"); }}
代码二:根据DFS的特点,由于棋盘每次只交换一对,所以可以只开一个棋盘数组。
#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;int M[100];int 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' };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) { 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;}char path[100];bool IDAstar(int loc, int depth, int pre, int limit){ int h = dis_h(M); if(depth+h>limit) return false; if(h==0) //搜到123456789 { path[depth] = '\0'; puts(path); return true; } int x = loc/3; int y = loc%3; for(int i = 0; i<4; i++) { if(i+pre==1 || i+pre==5) continue; //方向与上一步相反, 剪枝 int xx = x + dir[i][0]; int yy = y + dir[i][1]; if(xx>=0 && xx<=2 && yy>=0 && yy<=2) { swap(M[loc], M[xx*3+yy]); path[depth] = op[i]; if(IDAstar(xx*3+yy, depth+1, i, limit)) return true; swap(M[loc], M[xx*3+yy]); } } return false;}int main(){ char str[50]; while(gets(str)) { int cnt = 0, loc; for(int i = 0; str[i]; i++) { if(str[i]==' ') continue; if(str[i]=='x') M[cnt] = 9, loc = cnt++; else M[cnt++] = str[i]-'0'; } int limit; for(limit = 0; limit<=100; limit++) //迭代加深搜 if(IDAstar(loc, 0, INF, limit)) break; if(limit>100) puts("unsolvable"); }}
阅读全文
0 0
- POJ1077 Eight —— IDA*算法
- POJ1077 Eight —— A*算法
- 【POJ1077】Eight【IDA*】
- POJ1077——Eight
- HDU3567 Eight II —— IDA*算法
- 八数码 poj1077 Eight(A*、IDA*)
- poj1077 Eight —— 正向bfs+康拓
- POJ1077 Eight —— 正向BFS
- POJ1077 Eight —— 反向BFS
- POJ1077 Eight —— 双向BFS
- POJ1077 Eight —— 经典的搜索问题
- poj1077 Eight
- poj1077八数码问题——境界六(IDA*)
- POJ1077&HDU1043 Eight 八数码第八境界 IDA* hash 康托展开 奇偶剪枝
- poj1077 /hdoj 1043 Eight
- poj1077(A*,IDA*)
- Eight poj1077 广度优先搜索
- POJ 1077 Eight A*算法 IDA*算法 康拓展开
- [数论][二次剩余][BSGS] CodeChef FN
- MYSQL的索引类型:PRIMARY, INDEX,UNIQUE,FULLTEXT,SPAIAL 有什么区别?各适用于什么场合?
- 分布式id生成方案概述
- 离线下载demo
- 学习笔记---引入外部样式表时link与@import的区别
- POJ1077 Eight —— IDA*算法
- Restoring Painting
- [Leetcode] 393. UTF-8 Validation 解题报告
- Linux上JavaWeb环境搭建: JDK8 Tomcat Mysql Redis
- 抽奖
- 邝斌的ACM模板(RMQ )
- 面试
- WOJ1192-Image Conversion
- 组合数学基本工具-- 排列与组合以及简单公式