【题意】中文题面。
【分析&解题思路】简单BFS。
【AC代码】
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; struct node{ int x,y,z,time; }now,nex; int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}}; int n,m,t; char maze[2][11][11]; bool vis[2][11][11]; int bfs(); int main(){ int tt; cin>>tt; while(tt--){ scanf("%d%d%d",&n,&m,&t); memset(vis,false,sizeof(vis)); for(int k=0; k<2; k++){ for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ cin>>maze[k][i][j]; } } } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if((maze[0][i][j]=='#'&&maze[1][i][j]=='#')||(maze[0][i][j]=='#'&&maze[1][i][j]=='*')|(maze[0][i][j]=='*'&&maze[1][i][j]=='#')){ maze[0][i][j]=maze[1][i][j]='*'; } } } int ans = bfs(); if(ans==-1) puts("NO"); else puts("YES"); } return 0; } bool check(node x){ if(x.z>=0&&x.z<2&&x.x>=0&&x.x<n&&x.y>=0&&x.y<m&&maze[x.z][x.x][x.y]!='*') return true; return false; } int bfs(){ queue<node>qu; now.x=0,now.y=0,now.z=0; now.time=0; vis[0][0][0]=true; qu.push(now); while(!qu.empty()){ now=qu.front(); qu.pop(); if(now.time>t) return -1; if(maze[now.z][now.x][now.y]=='#'){ if(!vis[now.z^1][now.x][now.y]){ now.z^=1; vis[now.z][now.x][now.y]=true; } } if(maze[now.z][now.x][now.y]=='P') return now.time; for(int i=0; i<4; i++){ nex.z=now.z; nex.x=now.x+dir[i][0]; nex.y=now.y+dir[i][1]; nex.time=now.time+1; if(check(nex)&&!vis[nex.z][nex.x][nex.y]){ vis[nex.z][nex.x][nex.y]=true; qu.push(nex); } } } return -1; }