/*
---------------------------------------------------
ACMer : johnsondu
Time : 21:20 - 21:55, 2012.2.12
stratege : easy BFS
***************************************************
Problem : 2102 ( A计划 ) Judge Status : Accepted
RunId : 5345749 Language : C++ Author : a312745658
---------------------------------------------------
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <memory.h>
#include <queue>
using namespace std ;
char map[15][15][15] ;
int C, N, M, T ;
int sx, sy, sz ;
int ex, ey, ez ;
int direct[4][2] = {1, 0, 0, -1, -1, 0, 0, 1} ;
bool flag, mark[15][15][15] ;
struct Node
{
int x, y, z ;
int step ;
}n, m ;
int main()
{
int i, j, k ;
cin >> C ;
while (C --)
{
cin >> N >> M >> T ;
for (k = 0; k < 2; k ++)
for (i = 0; i < N; i ++)
for (j = 0; j < M; j ++)
{
cin >> map[k][i][j] ;
if (map[k][i][j] == 'P')
ex = i, ey = j, ez = k ;
if (map[k][i][j] == 'S')
sx = i, sy = j, sz = k ;
}
for (i = 0; i < N; i ++) //时光机当且仅当另一楼的状态为'.'有效,
for (j = 0; j < M; j ++) //其他的直接改变地图状态.
{
if (map[0][i][j] == '#' && map[1][i][j] == '*')
map[0][i][j] = map[1][i][j] = '*' ;
if (map[0][i][j] == '*' && map[1][i][j] == '#')
map[0][i][j] = map[1][i][j] = '*' ;
if (map[0][i][j] == '#' && map[1][i][j] == '#')
map[0][i][j] = map[1][i][j] ='*' ;
}
n.x = sx ;
n.y = sy ;
n.z = sz ;
n.step = 0 ;
flag = false ;
memset (mark, false, sizeof(mark)) ;
queue <Node> Q ;
Q.push (n) ;
mark[n.z][n.x][n.y] = true ;
while (! Q.empty())
{
m = Q.front () ;
Q.pop () ;
if (m.x == ex && m.y == ey && m.z == ez && m.step <= T) //循环截止条件
{
flag = true ;
break ;
}
for (i = 0; i < 4; i ++)
{
n.x = m.x + direct[i][0] ;
n.y = m.y + direct[i][1] ;
n.step = m.step + 1;
n.z = m.z ;
if (n.x >= 0 && n.y >= 0 && n.x < N && n.y < M && map[n.z][n.x][n.y] != '*' && !mark[n.z][n.x][n.y] && n.step <= T)
{ //当且仅当满足以上条件,为入队列的必要条件
mark[m.z][n.x][n.y] = true ;
if (map[m.z][n.x][n.y] == '#') //碰到时光机的情况,判断另一层是否可行
if (map[(n.z+1)%2][n.x][n.y] != '*' && !mark[(n.z+1)%2][n.x][n.y])
n.z = (m.z + 1) % 2 ; //可行改变楼层,注意:楼层只有0或者1,两种状态
Q.push (n) ;
}
}
}
if (flag)
printf ("YES\n") ;
else
printf ("NO\n") ;
}
return 0 ;
}
HDU 2102 A计划 BFS
最新推荐文章于 2024-10-16 05:00:00 发布