A计划(BFS)
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小NM(1 <= N,M <=10)。T如上所意。接下去的前NM表示迷宫的第一层的布置情况,后NM表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#…
…
****.
…#.
….P
#.…
**…
….
*.#…
Sample Output
YES
***note:***要注意#,判断传送后另一层的位置是不是#。用dfs好像一直在TLE,bfs就A了。
#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<deque>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const ll INF = 1ll << 62;
const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 998244353;
#define wait while(1){}
#define speed {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); }
inline int gcd(int a, int b)
{
while (b ^= a ^= b ^= a %= b);
return a;
}
int n,m,t;
char tu[2][15][15];
int vis[2][15][15];
int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
struct node
{
int deep,x,y,z;
node(int x,int y,int z,int deep=0):x(x),y(y),z(z),deep(deep){}
};
void init()
{
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
scanf("%c", &tu[0][i][j]);
}
getchar();
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
scanf("%c", &tu[1][i][j]);
}
}
bool check(int x,int y,int z)
{
if(x<0||x>=2||y<0||y>=n||z<0||z>=m||tu[x][y][z]=='*'||vis[x][y][z]==1)
return false;
return true;
}
int debug=-1;
bool bfs()
{
queue<node>q;
bool res=false;
q.push(node(0,0,0,0));
while(!q.empty())
{
node u=q.front();
q.pop();
vis[u.x][u.y][u.z]=1;
if(tu[u.x][u.y][u.z]=='P')
{
if(u.deep<=t)res=true;
else res=false;
debug=u.deep;
}
for(int i=0;i<4;i++)
{
int nx,ny,nz,nd;
if(tu[u.x][u.y][u.z]=='.'||tu[u.x][u.y][u.x]=='S')
{
nx=u.x;
ny=u.y+dir[i][0];
nz=u.z+dir[i][1];
nd=u.deep+1;
}
if(tu[u.x][u.y][u.z]=='#')
{
nx=u.x^1;;
ny=u.y;
nz=u.z;
nd=u.deep;
if(tu[nx][ny][nz]=='#')
continue;
}
if(check(nx,ny,nz))
q.push(node(nx,ny,nz,nd));
}
}
return res;
}
int main()
{
int C;
scanf("%d",&C);
while(C--)
{
for(int i=0;i<2;i++)
for(int j=0;j<n;j++)
for(int k=0;k<m;k++)
vis[i][j][k]=0;
scanf("%d %d %d",&n,&m,&t);
init();
if(bfs())printf("YES\n");
else printf("NO\n");
//printf("%d\n",debug);
}
//wait;
return 0;
}