A计划 HDU - 2102 (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
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
char a[2][20][20];
int vis[2][20][20];
int n,m,step;
struct node{
int fl;
int x;
int y;
int t;
};
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
queue<node> q;
int judge(int fl,int x,int y){
if(x>=n||y>=m||x<0||y<0) return 0;
else if(vis[fl][x][y]) return 0;
else if(a[fl][x][y]=='*') return 0;
else if(a[fl][x][y]=='#'&&(a[!fl][x][y]=='*'||a[!fl][x][y]=='#')) return 0;
}
int bfs(){
node t;
t.x=0,t.y=0,t.t=0;t.fl=0;
vis[0][0][0]=1;
q.push(t);
while(!q.empty()){
t=q.front();
q.pop();
int tx=t.x;
int ty=t.y;
int tt=t.t;
int tfl=t.fl;
// printf("a[%d][%d][%d]:%c %d\n",tfl,tx,ty,a[tfl][tx][ty],tt);
if(tt>step) return 0;
if(a[tfl][tx][ty]=='P') return 1;
for(int i=0;i<4;i++){
tx=t.x;
ty=t.y;
tt=t.t;
tfl=t.fl;
tx+=fx[i];
ty+=fy[i];
tt+=1;
if(judge(tfl,tx,ty)){
if(a[tfl][tx][ty]=='#'){
vis[tfl][tx][ty]=1;
tfl=!tfl;
}
vis[tfl][tx][ty]=1;
node tem;
tem.x=tx;tem.y=ty;tem.fl=tfl;tem.t=tt;
q.push(tem);
}
}
}
return 0;
}
int main(){
int c;
scanf("%d",&c);
while(c--){
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
scanf("%d%d%d",&n,&m,&step);
for(int i=0;i<n;i++){
scanf("%s",a[0][i]);
}
getchar();
for(int i=0;i<n;i++){
scanf("%s",a[1][i]);
// cout<<a[1][i];
}
if(bfs()) printf("YES\n");
else printf("NO\n");
}
return 0;
}