Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在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<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stack> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; struct Node { int x, y; int time; int flag; } q[100024]; int d[4][2]= { 0,1,1,0,0,-1,-1,0 }; int N,M; char map[2][13][13]; void getxy( int &X,int &Y,int &K ) { for( int k=0; k<2; k++ ) for( int i=1; i<=N; i++ ) for( int j=1; j<=M; j++ ) { if( map[k][i][j]=='S' ) { K=k; X=i; Y=j; return ; } } } int push( int x,int y,int time,int flag,int end ) { Node t; t.x=x; t.y=y; t.time=time; t.flag=flag; q[end++]=t; return end; } bool judge( int x,int y,int flag,int time ) { if( map[flag][x][y]!=0 && map[flag][x][y]!='*'&&time>=0 ) return true; else return false; } bool BFS( int x,int y,int k,int time ) { int hash[2][14][14]= {0},first=0,end=0; end=push( x,y,time,k,end ); hash[k][x][y]=1; while( first<end ) { if( map[q[first].flag][q[first].x][q[first].y]=='P' ) return true; for( int i=0; i<4; i++ ) { int dx=q[first].x + d[i][0]; int dy=q[first].y + d[i][1]; int flag=q[first].flag; int time=q[first].time-1; if( judge( dx,dy,flag,time )&&!hash[flag][dx][dy] ) { if( map[flag][dx][dy]!='#' ) { if( map[flag][dx][dy]=='P' ) { return true; } end=push( dx,dy,time,flag,end ); } hash[flag][dx][dy]=1; if( map[flag][dx][dy]=='#'&&!hash[(flag+1)%2][dx][dy]) if(map[( flag +1 )%2][dx][dy]!='*'&&map[(flag +1 )%2][dx][dy]!='#' ) { if( map[( flag +1 )%2][dx][dy]=='P' ) return true; hash[( flag+1 )%2][dx][dy]=1; end=push( dx,dy,time,(flag+1)%2,end ) ; } } } first++; } return false; } int main() { int n,T,X,Y,K; scanf( "%d",&n ); while( n-- ) { memset( map,0,sizeof( map ) ); scanf( "%d%d%d",&N,&M,&T ); for( int k=0; k<2; k++ ) for( int i=1; i <= N; i++ ) { scanf( "%s",map[k][i]+1 ); } getxy( X,Y,K ); if( BFS( X,Y,K,T ) ) printf( "YES\n" ); else printf( "NO\n" ); } }