POJ 3131 Cubic Eight-Puzzle 双向BFS + HASH
来源:互联网 发布:服务器防火墙软件 编辑:程序博客网 时间:2024/04/28 19:45
超赞的一道搜索题,麻烦到没朋友,不过思路还是很清楚的。
双向搜索的时候需要注意一个地方就是起始状态只有一个,目标状态最多的时候感觉会有512个,所以要控制两个方向搜索的层数。第一次知道双向搜索还能控制层数,简直点赞。
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <cmath>#include <stack>#include <map>#pragma comment(linker, "/STACK:1024000000");#define EPS (1e-8)#define LL long long#define ULL unsigned long long#define _LL __int64#define _INF 0x3f3f3f3f#define Mod 6000007using namespace std;char Map[5][5];int Judge(char a,char b,char c){ if(a == 'W') { if(b == 'R') return 0; return 5; } else if(a == 'R') { if(b == 'W') return 1; return 3; } else if(a == 'B') { if(b == 'R') return 2; return 4; } else return 6;}struct Q{ int sta,x,y,fl,ans;};int mark[3000010][2];int jx[] = { 0, 0, 1,-1};int jy[] = {-1, 1, 0, 0};int re[10][10];int bit[10];struct H{ int site,data,next;}st[Mod];int head[Mod];int Top_H;int Query(int data,int Top){ int temp = data%Mod; int p = head[temp]; for(;p != -1; p = st[p].next) { if(st[p].data == data) return st[p].site; } p = Top_H++; st[p].data = data; st[p].site = Top; st[p].next = head[temp]; head[temp] = p; return -1;}int bfs(int x,int y){ Top_H = 0; memset(mark,-1,sizeof(mark)); int Top = 0; int i,j; for(i = 0;i < Mod; ++i) head[i] = -1; int sta = 0; for(i = 0; i < 3; ++i) { for(j = 0; j < 3; ++j) if(i == x && j == y) sta += Judge('E','E','E')*bit[i*3+j+1]; else sta += Judge('W','B','R')*bit[i*3+j+1]; } Q f,t; queue<Q> q; f.sta = sta; f.ans = 0,f.x = x,f.y = y,f.fl = 0; q.push(f); Query(f.sta,Top); mark[Top][0] = 0; Top++; f.ans = 0; f.fl = 1; for(i = 0; i < 512; ++i) { sta = 0,x = 0,y = 0; for(j = 0; j < 9; ++j) { if(Map[x][y] == 'E') { f.x = x; f.y = y; sta += Judge('E','E','E')*bit[x*3+y+1]; } else if(i&(1<<j)) { if(Map[x][y] == 'W') sta += Judge('W','R','B')*bit[x*3+y+1]; else if(Map[x][y] == 'R') sta += Judge('R','B','W')*bit[x*3+y+1]; else sta += Judge('B','R','W')*bit[x*3+y+1]; } else { if(Map[x][y] == 'W') sta += Judge('W','B','R')*bit[x*3+y+1]; else if(Map[x][y] == 'R') sta += Judge('R','W','B')*bit[x*3+y+1]; else sta += Judge('B','W','R')*bit[x*3+y+1]; } y++; if(y == 3) y = 0,x++; } f.sta = sta; q.push(f); if(Query(f.sta,Top) == -1) { mark[Top++][1] = 0; } else { mark[Query(f.sta,Top)][1] = 0; } } int site,tsite,tdata; while(q.empty() == false) { f = q.front(); q.pop(); if((f.fl == 1 && f.ans > 9) || (f.fl == 0 && f.ans > 20)) continue; if(mark[Query(f.sta,Top)][f.fl^1] != -1) { return mark[Query(f.sta,Top)][f.fl^1] + f.ans; } site = f.x*3 + f.y; t.fl = f.fl; t.ans = f.ans+1; for(i = 0; i < 4; ++i) { t.x = f.x + jx[i]; t.y = f.y + jy[i]; if(0 <= t.x && t.x <= 2 && 0 <= t.y && t.y <= 2) { tsite = t.x*3+t.y; tdata = f.sta/bit[tsite+1]%7; t.sta = f.sta/bit[tsite]*bit[tsite] + 6*bit[tsite+1] + f.sta%bit[tsite+1]; t.sta = t.sta/bit[site]*bit[site] + re[tdata][i]*bit[site+1] + t.sta%bit[site+1]; if(Query(t.sta,Top) == -1) { mark[Top++][t.fl] = t.ans; q.push(t); } else if(mark[Query(t.sta,Top)][t.fl] == -1) { mark[Query(t.sta,Top)][t.fl] = t.ans; q.push(t); } } } } return -1;}int main(){ re[0][0] = 1; re[0][1] = 1; re[0][2] = 2; re[0][3] = 2; re[1][0] = 0; re[1][1] = 0; re[1][2] = 4; re[1][3] = 4; re[2][0] = 3; re[2][1] = 3; re[2][2] = 0; re[2][3] = 0; re[3][0] = 2; re[3][1] = 2; re[3][2] = 5; re[3][3] = 5; re[4][0] = 5; re[4][1] = 5; re[4][2] = 1; re[4][3] = 1; re[5][0] = 4; re[5][1] = 4; re[5][2] = 3; re[5][3] = 3; int i,j,x,y; bit[9] = 1; for(i = 8; i >= 0; --i) bit[i] = bit[i+1]*7; while(scanf("%d %d",&y,&x) && (x||y)) { for(i = 0; i < 3; ++i) { for(j = 0; j < 3; ++j) { scanf("%*c%c",&Map[i][j]); } } printf("%d\n",bfs(x-1,y-1)); } return 0;}
1 0
- POJ 3131 Cubic Eight-Puzzle 双向BFS + HASH
- POJ-3131-Cubic Eight-Puzzle(双向BFS+哈希)
- poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重
- poj 3131 Cubic Eight-Puzzle (双向bfs+深度控制 立体八数码)
- 搜索 POJ 3131 Cubic Eight-Puzzle
- POJ 3131 Cubic Eight-Puzzle 笔记
- UESTC1490 Eight Puzzle 双向BFS
- 1604 - Cubic Eight-Puzzle
- Cubic Eight-Puzzle UVA
- POJ 1077 Eight(BFS Hash)
- LA 3618 Cubic Eight-Puzzle
- POJ 1077 Eight(康拓展开 BFS 双向BFS)
- HDU 1043 && POJ 1077 Eight bfs || 双向bfs || A*搜索
- POJ:1077 Eight(双向BFS+判重)
- poj3131Cubic Eight-Puzzle(立体8数码,双向bfs+6进制压缩)
- poj 1077 Eight (BFS)
- POJ 1077 Eight BFS -
- CDOJ 414 Eight Puzzle 搜索 BFS
- 在内核窥视用户态
- 解决ScrollView下嵌套ListView、GridView显示不全的问题
- Android自定义属性,format详解
- Linq var 转为List
- 什么叫工作到位?
- POJ 3131 Cubic Eight-Puzzle 双向BFS + HASH
- 内存管理
- 调用系统相机照相,照完后没有调用onActivityResult
- 虚拟实验——安装XenServer 6.2(二)
- Swift - Lesson2(上)
- thinkphp js路径问题
- 一个简单的下载html页面的程序
- 【剑指offer】用两个栈实现队列
- /etc/ld.so.conf.d/目录下文件的作用