题意:
两层地图,只有通过时空传输机才能在两层之间移动,问能否在规定时间移动到'P'
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int maxn = 100 + 10;
char G[2][maxn][maxn];
int n,m,t;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
struct Point
{
int x,y,f,step;
};
bool check(Point p)
{
if(p.step > t || G[p.f][p.x][p.y] == '*' )
return 0;
return 1;
}
int bfs()
{
queue<Point> que;
Point now,next;
now.x = now.y = 1;
now.f = now.step = 0;
que.push(now);
while(!que.empty())
{
now = que.front();
que.pop();
if(G[now.f][now.x][now.y] == '*' ) continue;
if(G[now.f][now.x][now.y] == 'P' )
return 1;
G[now.f][now.x][now.y] = '*';
for(int i = 0; i < 4; i++)
{
next.x = now.x + dx[i];
next.y = now.y + dy[i];
next.step = now.step + 1;
next.f = now.f;
if(!check(next)) continue;
if(G[next.f][next.x][next.y] == '#')
{
G[next.f][next.x][next.y] = '*';
next.f = 1 - next.f;
if(G[next.f][next.x][next.y] == '#' || G[next.f][next.x][next.y]=='*')
{
G[next.f][next.x][next.y] = G[1-next.f][next.x][next.y] = '*';
continue;
}
}
que.push(next);
}
}
return 0;
}
int main()
{
int c;
for(cin >> c; c > 0; c--)
{
memset(G,'*',sizeof G);
cin>>n>>m>>t;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>G[0][i][j];
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>G[1][i][j];
}
}
if(bfs())
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}