可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是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思路:简单深搜,注意对地图的预处理。#include <stdio.h> #include <string.h> #include <queue> using namespace std; #define maxn 15 int Time,row,col,flag; char map[2][maxn][maxn]; bool visit[2][maxn][maxn]; struct node{ int cen,x,y; int step; }; int dx[]={-1,0,1,0}; int dy[]={0,1,0,-1}; bool bian(int x,int y) { if(x>=1 && x<=row && y>=1 && y<=col) return 1; return 0; } void bfs(int cen,int sx,int sy,int step) { int k,i,j,tx,ty; node from,to,head,tp; queue<node>Q; while(!Q.empty()) Q.pop(); from.cen=0; from.x=sx;from.y=sy;from.step=step; Q.push(from); visit[cen][sx][sy]=true; while(!Q.empty()) { head=Q.front(); Q.pop(); //printf("\n**************1\n"); if(map[head.cen][head.x][head.y]=='P'){ if(head.step<=Time) { flag=1;return ; } else return ; } // printf("\n**************2\n"); for(i=0;i<4;i++) { tx=head.x+dx[i]; ty=head.y+dy[i]; //printf("...%c<%d %d %d %d>....\n",map[head.cen][tx][ty],head.cen,tx,ty,head.step); if(bian(tx,ty) && !visit[head.cen][tx][ty] && map[head.cen][tx][ty]!='*') { if(map[head.cen][tx][ty]=='P'){ if(head.step+1<=Time) { flag=1;return ; } else return ; } else if(map[head.cen][tx][ty]=='.'){ tp.cen=head.cen; tp.x=tx; tp.y=ty; tp.step=head.step+1; Q.push(tp); visit[tp.cen][tx][ty]=true; } else if(map[head.cen][tx][ty]=='#' && !visit[(head.cen+1)%2][tx][ty]){ tp.cen=(head.cen+1)%2; tp.x=tx; tp.y=ty; tp.step=head.step+1; Q.push(tp); visit[head.cen][tx][ty]=true; visit[tp.cen][tx][ty]=true; } //printf("<<%d %d %d %d>>\n",tp.cen,tx,ty,tp.step); } } } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t,i,j; int stcen,stx,sty; scanf("%d",&t); while(t--) { scanf("%d %d %d",&row,&col,&Time); //printf("<<%d %d %d>>\n",row,col,Time); for(i=1;i<=row;i++) scanf("%s",&map[0][i][1]); for(i=1;i<=row;i++) scanf("%s",&map[1][i][1]); for(i=1;i<=row;i++) for(j=1;j<=col;j++) { if(map[0][i][j]=='S'){ stcen=0;stx=i;sty=j; } if(map[1][i][j]=='S'){ stcen=1;stx=i;sty=j; } if(map[0][i][j]=='#' && map[1][i][j]=='*'){ map[0][i][j]='*'; continue; } if(map[1][i][j]=='#'&& map[0][i][j]=='*') { map[1][i][j]='*'; continue; } if(map[1][i][j]=='#' && map[0][i][j]=='#'){ map[0][i][j]=map[1][i][j]='*'; continue; } } flag=0; memset(visit,false,sizeof(visit)); bfs(stcen,stx,sty,0); if(flag) printf("YES\n"); else printf("NO\n"); } }