Title: http://acm.hdu.edu.cn/showproblem.php?pid=2102
Knight Rescue Princess, Maze problem. Do when the idea is not clear, has been WA, in fact, is not clear from POSA-->POSB whether B is the portal or the road will take 1, but through the portal and transmitted to the next position; has been in this misunderstanding, Tmdi.
BFS Search again on the description of the search Posa must be a point, the code and the special circumstances are all excluded,
So there are only two things:
'. '---> ' # ',
‘.‘ --'. ' (contains P, which is the search end condition),
It's a lot of clear moments.
AC Code:
#include <queue>#include<cstdio>#include<cstring>#include<iostream>using namespacestd;Charmap[2][ A][ A];intac[4][2] = {0,1,0, -1, -1,0,1,0};intN, M, T;structmaze{intx, y, z, step; } R, S, T;BOOLBfs (intXintYintz) {r.x= x; R.y = y; R.z = Z; R.step =0; Queue<Maze>Q; Q.push (R); MAP[X][Y][Z]='*'; while(!Q.empty ()) {s=Q.front (); Q.pop (); for(inti =0; I <4; i++) {T=s; T.y= S.y + ac[i][0]; T.z= S.z + ac[i][1]; T.step= S.step +1; if(T.z >=0&& T.y >=0&& T.y < n && t.z < m && Map[t.x][t.y][t.z]! ='*') { if(Map[t.x][t.y][t.z] = ='#') {Map[t.x][t.y][t.z]='*';//mark as ' * ', because the next mark is sent to the place, not marking is a break;T.x =!s.x; } if(Map[t.x][t.y][t.z] = ='P'&& T.step <=T)return true; MAP[T.X][T.Y][T.Z]='*'; Q.push (t); } } } return false;} intMain () {intQi; scanf ("%d", &Qi); while(qi--) {scanf (" %d%d%d", &n, &m, &T); for(inti =0; I <2; i++) for(intj =0; J < N; J + +) for(intK =0; K < M; k++) Cin>>Map[i][j][k]; for(inti =0; I < n; i++) for(intj =0; J < M; J + +)//Replace the test instructions of the road with ' * '; { if(map[0][I][J] = ='#'&& map[1][I][J] = ='#')//the top and bottom two layers of the same position are the portal;{map[0][I][J] ='*'; map[1][I][J] ='*'; } if(map[0][I][J] = ='#'&& map[1][I][J] = ='*')//The upper and lower layers of the same position at the same level as the portal, one layer for the wall;{map[0][I][J] ='*'; } if(map[0][I][J] = ='*'&& map[1][I][J] = ='#')//Ibid.{map[1][I][J] ='#'; } } if(Bfs (0,0,0)) printf ("yes\n"); Elseprintf ("no\n"); } return 0; }
Hangzhou Electric 2102--a Project (BFS)