pku 1077 Eight【八数码、广搜、hash判重】
来源:互联网 发布:淘宝红枸杞包装盒 编辑:程序博客网 时间:2024/05/01 23:07
pku 1077 Eight(八数码) (广搜+hash判重)/*memcpy(&t,s,sizeof(s))&t: 目标; s: 源 ; sizeof(s): 源内存出复制出的大小 作用: 由s所指内存区域复制sizeof(s)个字节到t所指内存区域*//*memcmp(st[u],st[s],sizeof(st[s]))st[u]: 目标; st[s]: 源 ; sizeof(st[s]): 源内 存出复制出的大小 作用: 比较内存区域st[u]和st[s]的前sizeof(st[s])个字节*/ #include<iostream>#include<set> //集合 using namespace std;typedef int State[9];const int MAXSTATE = 1000000;State st[MAXSTATE], goal;int dist[MAXSTATE],path[MAXSTATE][2];int fa[MAXSTATE][2];/* 如果需要打印,记录父亲编号(fa[MAXSTATE][0]),及路径(fa [MAXSTATE][1])*/ /*节点查找表 哈希判重*********************************************************/const int MAXHASHSIZE = 1000003;int head[MAXHASHSIZE], next[MAXSTATE];void init_lookup_table() { memset(head, 0, sizeof(head)); }int hash(State& s) { int v = 0; for(int i = 0; i < 9; i++) v = v * 10 + s[i]; return v % MAXHASHSIZE;}int try_to_insert(int s) { int h = hash(st[s]); int u = head[h]; while(u) //从表头开始查找链表 { if(memcmp(st[u], st[s], sizeof(st[s])) == 0) return 0; //找到了 插入失败 u = next[u]; //顺着链表继续找 } next[s] = head[h]; //插入到链表中 head[h] = s; return 1;}/*节点查找表 哈希判重*********************************************************//*按上,下,左,右顺序走*/ const int dx[] = {-1, 1, 0, 0};const int dy[] = {0, 0, -1, 1};/*广搜*/ int bfs() { init_lookup_table(); int front = 1, rear = 2; fa[1][0]=-1; while(front < rear) { State& s = st[front]; if(memcmp(goal, s, sizeof(s)) == 0) return front; int z; for(z = 0; z < 9; z++) if(!s[z]) break; int x = z/3, y = z%3; for(int d = 0; d < 4; d++) { int newx = x + dx[d]; int newy = y + dy[d]; int newz = newx * 3 + newy; if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3) { State& t = st[rear]; memcpy(&t, &s, sizeof(s)); t[newz] = s[z]; t[z] = s[newz]; dist[rear] = dist[front] + 1; //fa[rear][0]=front; if(try_to_insert(rear)) rear++,fa[rear-1][0]=front,fa[rear-1][1]=d; } } front++; } return 0;}int main() { char c; for(int i = 0;i < 9;) { scanf("%c", &c); if(c==' ') continue; if(c=='x') {st[1][i]=0;i++;continue;} st[1][i]=(int)(c-'0'); //从1开始 i++; } for(int i = 0; i < 9; i++) goal[i]=(i+1)%9; int ans = bfs(); for(int i=ans,k=dist[ans];;) { path[k][0]=fa[i][0]; path[k][1]=fa[i][1]; k--; if(fa[i][0]==1) break; i=fa[i][0]; } /*dist[ans]:最小步数*/ for(int i=1;i<=dist[ans];i++) { if(path[i][1]==0) printf("u"); if(path[i][1]==1) printf("d"); if(path[i][1]==2) printf("l"); if(path[i][1]==3) printf("r"); } printf("\n");}参考刘汝佳代码 本人只增加了记录路径的功能
- pku 1077 Eight【八数码、广搜、hash判重】
- PKU-1077 Eight (八数码单向BFS+HASH)
- POJ 1077 Eight(八数码) 双向广搜
- PKU-1077 Eight (八数码之IDA*)
- poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重
- 基于八数码问题的hash判重
- 八数码问题(bfs,hash判重)
- PKU-1077 Eight (八数码之双向BFS)
- PKU-1077 Eight (八数码之A*算法)
- POJ 1077 Eight(八数码第四境界|双向广搜+哈希)
- HDU 1043 Eight ((八数码问题)逆向BFS + 康托定理判重)
- 1077 Eight 八数码问题
- UVA 1533 - Moving Pegs (广搜+hash判重)
- hoj 1868 八数码(双广+hash)
- HDU 1043 Eight(八数码第三境界|广搜+哈希+打表)
- POJ1077 HDU1043 Eight 八数码第四境界 双向广搜 康托展开 逆康托
- hdu 1043-Eight(经典八数码问题)(单向广搜 A* 状态压缩)
- 关于八数码问题中的状态判重的三种解决方法(编码、hash、<set>)
- MyEclipse下集成jad编译工具
- hdu 1556 Color the ball 树状数组
- java.net.Url类的应用
- 关于测试用例一些思考
- java.net.InetAddress类的应用
- pku 1077 Eight【八数码、广搜、hash判重】
- MVVM设计模式
- excel 打开时出现向程序发送命令出错
- undefined reference to 与 /usr/lib/ld: cannot find -l
- 黑马程序员-注解
- Linux动态链接库搜索路径
- LINUX下处理端口被占用
- django 创建一个通用视图
- 收集的一些资源