超赞的一道搜索题,麻烦到没朋友,不过思路还是很清楚的。
双向搜索的时候需要注意一个地方就是起始状态只有一个,目标状态最多的时候感觉会有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 6000007 using 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; }
POJ 3131 Cubic Eight-Puzzle 双向BFS + HASH,布布扣,bubuko.com
POJ 3131 Cubic Eight-Puzzle 双向BFS + HASH
原文地址:http://blog.csdn.net/zmx354/article/details/36187339