Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
1
5 5 14
S*
.
.....
****.
...
..*.P
***..
...*.
*.
Sample Output
YES
题目大意
中文题
思路
三维的bfs,预处理以下几种情况就好: ①上下两层都是‘#’,那么相当于死路,上下两层的点都预处理为‘*’; ②两层中有一层是‘#’,另一层是‘’,那么会撞死,同样上下两层的点预处理为‘’;
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=
10+
5;
int dir[
4][
2]={{
1,
0},{
0,
1},{-
1,
0},{
0,-
1}};
char map[maxn][maxn][
2];
int n,m,t;
bool vis[maxn][maxn][
2];
int posx,posy,posz;
struct proc
{
int x,y,z;
int step;
};
bool check(
int x,
int y,
int z)
{
if(x>=
0&&x<n&&y>=
0&&y<m&&z>=
0&&z<
2&&!vis[x][y][z]&&
map[x][y][z]!=
'*')
return 1;
else return 0;
}
int bfs(
int x,
int y,
int z)
{
proc vw,vn;
queue<proc> q;
vw.x=x;
vw.y=y;
vw.z=z;
vw.step=
0;
vis[x][y][z]=
1;
q.push(vw);
while(!q.empty())
{
vw=q.front();
q.pop();
if(vw.x==posx&&vw.y==posy&&vw.z==posz&&vw.step<=t)
{
return true;
}
for(
int i=
0;i<
4;i++)
{
vn.x=vw.x+dir[i][
0];
vn.y=vw.y+dir[i][
1];
vn.z=vw.z;
vn.step=vw.step+
1;
if(check(vn.x,vn.y,vn.z)&&!vis[vn.x][vn.y][vn.z]&&vn.step<=t)
{
vis[vn.x][vn.y][vn.z]=
1;
if(
map[vn.x][vn.y][vn.z]==
'#')
{
if(
map[vn.x][vn.y][(vn.z+
1)%
2]!=
'*'&&!vis[vn.x][vn.y][(vn.z+
1)%
2])
{
vn.z=(vn.z+
1)%
2;
}
}
q.push(vn);
}
}
}
return -
1;
}
int main()
{
int c;
cin>>c;
while(c--)
{
memset(vis,
0,
sizeof(vis));
cin>>n>>m>>t;
char cc;
for(
int z=
0;z<
2;z++)
{
for(
int i=
0;i<n;i++)
{
for(
int j=
0;j<m;j++)
{
cin>>cc;
map[i][j][z]=cc;
if(
map[i][j][z]==
'P')
{
posx=i;
posy=j;
posz=z;
}
}
}
}
for(
int i=
0;i<n;i++)
{
for(
int j=
0;j<m;j++)
{
if(
map[i][j][
0]==
'#'&&
map[i][j][
1]==
'*')
{
map[i][j][
0]=
map[i][j][
1]=
'*';
}
if(
map[i][j][
0]==
'*'&&
map[i][j][
1]==
'#')
{
map[i][j][
0]=
map[i][j][
1]=
'*';
}
if(
map[i][j][
0]==
'#'&&
map[i][j][
1]==
'#')
{
map[i][j][
0]=
map[i][j][
1]=
'*';
}
}
}
int ans=bfs(
0,
0,
0);
if(ans+
1)
printf(
"YES\n");
else printf(
"NO\n");
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-1302865.html