题目链接
题意
在一个两层迷宫内,骑士从起点是否能在给定步数之内找到公主,’#’代表传送门,会传送到另一个平面该位置去,’*’代表墙,’P’代表公主
解题思路
水题,用bfs模拟即可,就是有个坑 传送门被传到的位置不能是墙(由题意)和传送门(会造成无限传送)
AC代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #include<vector> #include<algorithm> #include<cstdio> #include<iostream> #include<set> #include<cstring> #include<functional> #include<map> #include<cmath> #include<string> #include<queue> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<int,pii> PII; const int maxn = 15; int N,M,S; struct Node { int x,y,z; int step; }; char G[2][maxn][maxn];; bool vis[2][maxn][maxn]; bool ok = 0; int dx[] = {1,0,-1,0}; int dy[] = {0,1,0,-1};
void bfs(Node beg) { queue<Node> Q; Q.push(beg); while(!Q.empty()){ Node t = Q.front(); Q.pop(); if(t.step > S)continue; if(G[t.z][t.x][t.y]==‘P’){ ok = 1; return ; } for(int i=0;i<4;i++){ int nx = t.x + dx[i]; int ny = t.y + dy[i]; int nz = t.z; if(nx<0||ny<0||ny>=M||nx>=N)continue; if(G[nz][nx][ny]==‘‘)continue; if(vis[nz][nx][ny])continue; vis[nz][nx][ny] = 1; if(G[nz][nx][ny]==‘#’&&G[nz^1][nx][ny]!=‘‘&&G[nz^1][nx][ny]!=‘#’){ Q.push((Node){nx,ny,nz^1,t.step+1}); }else if(G[nz][nx][ny]!=‘#’) Q.push((Node){nx,ny,nz,t.step+1}); } } }
int main(int argc, char const *argv[]) { int T = 0; cin >> T; while(T–){ ok = 0; memset(vis,0,sizeof(vis)); cin >> N >> M >> S; Node beg; for (int k = 0; k < 2; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> G[k][i][j]; } } } beg = (Node){0,0,0,0}; bfs(beg); if(ok){ cout << “YES” << endl; }else{ cout << “NO” << endl; } } return 0; }
|