Topic Portal
1 /*2 BFS: This is very interesting, like the underground city, the figure is three-dimensional, can be from the previous map to the next map of the corresponding position, then the three-dimensional search, more Z axis3 */4#include <cstdio>5#include <algorithm>6#include <cstring>7#include <queue>8 using namespacestd;9 Ten Const intMAXN = -; One Const intINF =0x3f3f3f3f; A structPoint { - intx, y, z, step; - }; the CharMAZE[MAXN][MAXN][MAXN]; - intdx[6] = {-1,1,0,0,0,0}; - intdy[6] = {0,0, -1,1,0,0}; - intdz[6] = {0,0,0,0, -1,1}; + BOOLVIS[MAXN][MAXN][MAXN]; - intL, R, C; + A BOOLJudgeintXintYintz) { at if(X <1|| X > R | | Y <1|| Y > C | | Z <1|| Z > L | | Vis[z][x][y] | | Maze[z][x][y] = ='#')return false; - return true; - } - - voidBFS (void) { - intSX, SY, SZ, ex, EY, EZ; in for(intI=1; i<=l; ++i) { - for(intj=1; j<=r; ++j) { to for(intk=1; k<=c; ++k) { + if(Maze[i][j][k] = ='S') { -SX = j; sy = k; SZ =i; the } * Else if(Maze[i][j][k] = ='E') { $ex = j; ey = k; EZ =i;Panax Notoginseng } - } the } + } Amemset (Vis,false,sizeof(Vis)); theQueue<point> Q; Q.push (point) {SX, SY, SZ,0}); + BOOLFlag =false; Vis[sz][sx][sy] =true; - while(!Q.empty ()) { $Point P =Q.front (); Q.pop (); $ if(p.x = = Ex && p.y = = ey && P.z = =ez) { -printf ("escaped in%d minute (s). \ n", p.step); -Flag =true; Break; the } - for(intI=0; i<6; ++i) {Wuyi inttx = p.x + dx[i];intty = p.y + dy[i];intTZ = P.z +Dz[i]; the if(Judge (TX, Ty, TZ)) { -Vis[tz][tx][ty] =true; WuQ.push (Point {tx, Ty, TZ, P.step +1}); - } About } $ } - if(!flag) puts ("trapped!"); - } - A intMainvoid) {//POJ 2251 Dungeon Master + while(SCANF ("%d%d%d", &l, &r, &c) = =3) { the if(!l &&!r &&!c) Break; - for(intI=1; i<=l; ++i) { $ for(intj=1; j<=r; ++J) scanf ("%s", Maze[i][j] +1); the } the BFS (); the } the - return 0; in}
BFS POJ 2251 Dungeon Master