Topic Link: Portal
Test instructions
A three-dimensional maze, starting at the first level of S (0,0,0), ask whether it is possible to go to the second layer of p within the specified time
Secretariat ' * ' means not to go, '. ' The representative can walk, ' # ' stands for the portal, here is a trick, when it comes to the portal
Must be transmitted. Didn't notice WA a lot of times before.
And at the beginning of the map can be processed, (' * ', ' # '), (' # ', ' * '), (' # ', ' # ')such a certainty
is not allowed to go, so you can turn them into ' * '
The code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < queue>using namespace Std;const int maxn = 50;char mp[2][maxn][maxn];int vis[2][maxn][maxn];int dx[4]= {1,-1,0,0};int dy[4]= {0,0,-1,1};int n,m,limit;struct nod {int x, y, Z; int step; Nod () {} nod (int _x,int _y,int _z): X (_x), Y (_y), Z (_z) {}};bool check (nod tmp) {if (vis[tmp.z][tmp.x][tmp.y]) return FA Lse if (tmp.x>=0&&tmp.x<n&&tmp.y>=0&&tmp.y<m&&mp[tmp.z][tmp.x][tmp.y]!= ' * ') return true; return false;} BOOL BFs () {nod st=nod (0,0,0); Vis[0][0][0]=1; St.step=0; Queue<nod> Q; Q.push (ST); while (! Q.empty ()) {nod tmp = Q.front (); cout<<tmp.z<< "<<tmp.x<<" "<<tmp.y<<" "<<tmp.step<<endl; Q.pop (); if (mp[tmp.z][tmp.x][tmp.y]== ' P ' &&tmp.step<=limit) {return true; } else if (Mp[tmp.z][tmp.x][tmp.y]== ' P ') {return false; } for (int i=0; i<4&&mp[tmp.z][tmp.x][tmp.y]!= ' # '; i++) {nod top; top.x=tmp.x+dx[i],top.y=tmp.y+dy[i],top.z=tmp.z,top.step=tmp.step+1; if (check (top)) {Q.push (top); Vis[top.z][top.x][top.y]=1; }} if (mp[tmp.z][tmp.x][tmp.y]== ' # ') {tmp.z^=1; Q.push (TMP); Vis[tmp.z][tmp.x][tmp.y]=1; }} return false;} int main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &n,&m,&limit); for (int i=0; i<n; i++) scanf ("%s", Mp[0][i]); for (int i=0; i<n; i++) scanf ("%s", Mp[1][i]); for (int i=0, i<n; i++) {for (int j=0; j<m; J + +) {if (mp[0][i][j]== ' # ' &&mp[1][i][j] = = ' # ') mp[0][i][j]=mp[1][i][j]= ' * '; if (mp[0][i][j]== ' # ' &&mp[1][i][j]== ' * ') mp[0][i][j]=mp[1][i][j]= ' * '; if (mp[0][i][j]== ' * ' &&mp[1][i][j]== ' # ') mp[0][i][j]=mp[1][i][j]= ' * '; }} memset (Vis,0,sizeof (VIS)); if (BFS ())) puts ("YES"); Else puts ("NO"); } return 0;}
HDU 2102 A plan (three-dimensional labyrinth BFS)