注意:
bfs+判断,类似与hdu1080。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
char s[20];
struct node{
int z,x,y,t;
node(int zz = 0,int xx = 0,int yy = 0,int tt = 0){
z = zz,x = xx,y = yy,t = tt;
}
};
int v[2][20][20];
int d[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
int bfs(){
node tem(0,1,1,0);
queue<node> q;
q.push(tem);
while(!q.empty()){
tem = q.front();
q.pop();
for(int i = 0;i < 4;i++){
int a = tem.x + d[i][0],b = tem.y + d[i][1];
if(v[tem.z][a][b] == 1){
v[tem.z][a][b] = 0;
q.push(node(tem.z,a,b,tem.t+1));
}
else if(v[tem.z][a][b] == 2){
int newz = tem.z ^ 1;
if(v[newz][a][b] != 0 && v[newz][a][b] != 2){
if(v[newz][a][b] == 3)
return tem.t+1;// 注意是t+1
else{
v[newz][a][b] = 0;
q.push(node(newz,a,b,tem.t+1));// 注意是t+1
}
}
}
else if(v[tem.z][a][b] == 3){
return tem.t + 1;
}
}
}
return 2333333;
}
int main(){
int cas;
cin>>cas;
int m,n,t;
while(cas--){
memset(v,0,sizeof(v));
scanf("%d%d%d",&m,&n,&t);
for(int i = 0;i <= 1;i++){
for(int j = 1;j <= m;j++){
scanf("%s",s);
for(int k = 0;k < n;k++){
if(s[k] == 'P'){
v[i][j][k+1] = 3;
}
else if(s[k] == '.'){
v[i][j][k+1] = 1;
}
else if(s[k] == '#'){
v[i][j][k+1] = 2;
}
}
}
}
int ans = bfs();
if(ans <= t)
puts("YES");
else
puts("NO");
}
return 0;
}