IDA* 解八数码问题: POJ 1077
2015-10-07 21:23
423 查看
刚刚学IDA*,再来写一遍八数码问题。又一次见证了别人家的代码和自己的代码的差距。
别人家的链接:http://www.xuebuyuan.com/1863051.html
h函数自己一开始写的是不在正确位置上的数码的个数,性能低下,T。
更优的一个,也是很多人用的一个就是所有数码与正确位置的曼哈顿距离之和。
然而自己写这个还是T,看了别人家的代码中有个小剪枝,加上后用C++编译还是T,使用G++,1000Ms过。真是压线。
人家的是0Ms。
注:所说的小剪枝是相邻的两步不来回走,在原博客中有代码因为长度过长导致一部分没有显示出来粘贴到Dev上注释会变成乱码
别人家的链接:http://www.xuebuyuan.com/1863051.html
h函数自己一开始写的是不在正确位置上的数码的个数,性能低下,T。
更优的一个,也是很多人用的一个就是所有数码与正确位置的曼哈顿距离之和。
然而自己写这个还是T,看了别人家的代码中有个小剪枝,加上后用C++编译还是T,使用G++,1000Ms过。真是压线。
人家的是0Ms。
注:所说的小剪枝是相邻的两步不来回走,在原博客中有代码因为长度过长导致一部分没有显示出来粘贴到Dev上注释会变成乱码
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; int maxd, mi = 100, beg[10], n[10]; char ans[105]; int h(){ int res = 0; for(int i = 1; i <= 9; i++){ if(n[i] < 9){ int x = (i-1)/3 + 1; int y = i % 3; if(!y) y = 3; int x2 = (n[i]-1)/3 + 1; int y2 = n[i] % 3; if(!y2) y2 = 3; res += abs(x-x2)+abs(y-y2); } } mi = min(mi, res); return res; } bool dfs(int ed){ if(!h()){ ans[ed] = '\0'; printf("%s\n", ans); return 1; } if(ed + h() >= maxd) return 0; int pos = 0; while(n[pos] < 9) pos++; if(pos % 3){ swap(n[pos], n[pos+1]); ans[ed] = 'r'; if(dfs(ed+1)) return 1; swap(n[pos], n[pos+1]); } if(pos <= 6){ swap(n[pos], n[pos+3]); ans[ed] = 'd'; if(dfs(ed+1)) return 1; swap(n[pos], n[pos+3]); } if(pos > 3){ swap(n[pos], n[pos-3]); ans[ed] = 'u'; if(dfs(ed+1)) return 1; swap(n[pos], n[pos-3]); } if(pos % 3 != 1){ swap(n[pos], n[pos-1]); ans[ed] = 'l'; if(dfs(ed+1)) return 1; swap(n[pos], n[pos-1]); } return 0; } int main() { for(int i = 1; i <= 9; i++){ char s[2]; scanf("%s", s); if(s[0] != 'x') beg[i] = s[0] - 48; else beg[i] = 9; } int ni = 0; for(int i = 1; i < 9; i++) for(int j = i+1; j <= 9; j++){ ni += beg[i] > beg[j] && beg[i] != 9; } if(ni&1){ puts("unsolvable"); return 0; } for(maxd = h(); ; maxd+=mi){ for(int i = 1; i <= 9; i++) n[i] = beg[i]; if(dfs(0)) return 0; } }
相关文章推荐
- POJ ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题
- POJ 1088 滑雪
- poj2387 Til the Cows Come Home—Dijkstra模板
- poj 2485 Highways