A计划
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
Sample Output
YES
题意:不说啥了,这个题的坑点太多,我WA+TL8次,建议看到此篇博客的人去hdu discuss去看一下。。。。
思路:裸bfs
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<utility>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#define maxn 12
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define E 1e-8
#define mod 1000000007
#define P pair<int,int>
#define MID(l,r) (l+(r-l)/2)
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)
using namespace std;
int n,m,t;
char maze[3][maxn][maxn];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int vis[3][maxn][maxn];
struct node{
int x,y,z;
int step;
};
int bfs()
{
queue<node> q;
node u;
u.x = 1;u.y = 1;u.z = 1;u.step = 0;
q.push(u);
while(!q.empty()){
node nows = q.front();
q.pop();
// printf("nx,ny,nz:%d %d %d %d\n",nows.x,nows.y,nows.z,nows.step);
if(maze[nows.z][nows.x][nows.y] == 'P') return nows.step;
if(nows.step>t) return -1;
vis[nows.z][nows.x][nows.y] = 1;
for(int i=0;i<4;++i){
int nx = nows.x + dir[i][0];
int ny = nows.y + dir[i][1];
int nz = nows.z;
if(vis[nz][nx][ny] == 1) continue ;
if(nx<1||nx>n||ny<1||ny>m) continue ;
if(maze[nz][nx][ny]=='*') continue ;
if(maze[nz][nx][ny]=='#'){
nz == 1?(nz = 2):nz = 1;
if(maze[nz][nx][ny]=='*'||maze[nz][nx][ny]=='#'||vis[nz][nx][ny]==1) continue ;
}
node v;
v.x = nx;v.y = ny;v.z = nz;v.step = nows.step+1;
q.push(v);
}
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
scanf("%d %d %d",&n,&m,&t);
for(int l=1;l<=2;++l){
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
cin>>maze[l][i][j];
}
}
}
int ans = bfs();
// printf("%d\n",ans);
if(ans==-1 || ans>t) printf("NO\n");
else printf("YES\n");
}
return 0;
}