#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct node{
int index,x,y,time;
friend bool operator < (node a,node b){
return a.time>b.time;
}
};
char graph[2][11][11];
bool vis[2][11][11];
int t,n,m;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int bfs(){
priority_queue<node> q;
node cur,next;
memset(vis,false,sizeof(vis));
cur.index = 0;
cur.time = 0;
cur.x = 0;
cur.y = 0;
vis[0][0][0] = true;
q.push(cur);
while(!q.empty()){
cur = q.top();
q.pop();
if(cur.time > t) //若当前队列中时间最短的都超时,则所有节点均超时
return -1;
if(graph[cur.index][cur.x][cur.y]=='P')
return 1; //到达目标状态
for(int i=0;i<4;i++){
next = cur;
next.x += dx[i];
next.y += dy[i];
int idx = next.index;
int x = next.x;
int y =next.y;
if(!vis[idx][x][y] && x>=0 && x<n && y>=0 && y<m && graph[idx][x][y]!='*'){
vis[idx][x][y] = 1;
if(graph[idx][x][y]=='#' && !vis[!idx][x][y]){ //传送机
if(graph[!idx][x][y]=='#' || graph[!idx][x][y]=='*')
continue;
next.index = !idx;
vis[next.index][next.x][next.y] = 1;
next.time = cur.time+1;
q.push(next);
}
else{
next.time = cur.time+1;
q.push(next);
}
}
}
}
return -1;
}
int main(){
int tt;
scanf("%d",&tt);
while(tt--){
scanf("%d%d%d",&n,&m,&t);
for(int i=0;i<n;i++)
scanf("%s",graph[0][i]);
for(int i=0;i<n;i++)
scanf("%s",graph[1][i]);
int res = bfs();
if(res!=-1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
HDU 2102
最新推荐文章于 2020-01-26 22:02:45 发布