题目比较简单,只是一个简单搜索,只是添加了一点条件。传输机实现上下层转移,仅仅这个条件。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int f[4][2] = { { -1, 0}, {0, 1}, {1, 0}, {0, -1}};
char Map[15][15][2];
bool vis[15][15][2];
struct pos {
int x, y, z, step;
};
bool BFS ( int n, int m, int t ) {
pos st;
st.step = 0, st.x = 0, st.y = 0, st.z = 0;
vis[st.x][st.y][st.z] = 1;
queue<pos>Q;
Q.push ( st );
while ( !Q.empty() ) {
pos tmp;
tmp.x = Q.front().x , tmp.step = Q.front().step ;
tmp.y = Q.front().y , tmp.z = Q.front().z;
// cout<<Map[tmp.x][tmp.y][tmp.z]<<" "<<tmp.x<<" "<<tmp.y<<" "<<tmp.z<<" "<<tmp.step<<endl;
if ( Map[tmp.x][tmp.y][tmp.z] == 'P' && tmp.step <= t ) {
return true;
}
pos temp;
for ( int i = 0; i < 4; i++ ) {
temp.x = tmp.x + f[i][0];
temp.y = tmp.y + f[i][1];
temp.z = tmp.z;
temp.step = tmp.step + 1;
if ( temp.x >= 0 && temp.x < n && temp.y >= 0 && temp.y < m && temp.z >= 0 && temp.z < 2 && temp.step <= t && !vis[temp.x][temp.y][temp.z] ) {
vis[temp.x][temp.y][temp.z] = 1;
if ( Map[temp.x][temp.y][temp.z] == '.' || Map[temp.x][temp.y][temp.z] == 'P' ) {
Q.push ( temp );
vis[temp.x][temp.y][temp.z] = 1;
}
if ( Map[temp.x][temp.y][temp.z] == '#' ) {
if ( temp.z == 0 )
temp.z = 1;
else
temp.z = 0;
if ( Map[temp.x][temp.y][temp.z] == '.' || Map[temp.x][temp.y][temp.z] == 'P' && !vis[temp.x][temp.y][temp.z] ) {
Q.push ( temp );
}
}
}
}
Q.pop();
}
return false;
}
int main() {
int C, N, M , T;
scanf ( "%d", &C );
while ( C-- ) {
memset ( vis, 0, sizeof ( vis ) );
cin >> N >> M >> T;
for ( int k = 0; k < 2; k++ ) {
for ( int i = 0; i < N; i++ ) {
for ( int j = 0; j < M; j++ ) {
cin >> Map[i][j][k];
}
}
}
bool ans = BFS ( N, M, T );
if ( ans ) {
printf ( "YES\n" );
}
else
printf ( "NO\n" );
}
return 0;
}