A计划
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22219 Accepted Submission(s): 5579
Problem Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在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
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
struct node
{
int x,y,floor;
int step;
};
int t,n,m,lim;
int s[3],e[3];
int to[4][2] = {1,0,0,1,-1,0,0,-1};//方向
char map[2][15][15];//地图 int vis[2][15][15];//访问标志
int check(int floor,int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m)
return 1;
if(map[floor][x][y]=='*')
return 1;
return 0;
}
void bfs()
{
memset(vis,0,sizeof(vis));
node a,next;
queue<node> Q;
int i,j,k;
a.floor = s[0];
a.x = s[1];
a.y = s[2];
a.step = 0;
vis[s[0]][s[1]][s[2]] = 1;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(a.floor == e[0] && a.x == e[1] && a.y == e[2])
{
printf("YES\n");
return ;
}
if(a.step>=lim)
break;
for(i = 0; i<4; i++)
{
next = a;
next.x+=to[i][0];
next.y+=to[i][1];
next.step++;
if(check(next.floor,next.x,next.y))
continue;
if(vis[next.floor][next.x][next.y])
continue;
vis[next.floor][next.x][next.y] = 1;
if(map[next.floor][next.x][next.y] == '#')
{
next.floor=!next.floor;
if(check(next.floor,next.x,next.y))
continue;
if(vis[next.floor][next.x][next.y])
continue;
if(map[next.floor][next.x][next.y] == '*' || map[next.floor][next.x][next.y] == '#')
continue;
vis[next.floor][next.x][next.y] = 1;
}
if(next.floor == e[0] && next.x == e[1] && next.y == e[2])
{
printf("YES\n");
return ;
}
Q.push(next);
}
}
printf("NO\n");
}
int main()
{
int i,j,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&lim);
for(k = 0; k<2; k++)
{
for(i = 0; i<n; i++)
{
scanf("%s",map[k][i]);
for(j = 0; j<m; j++)
{
if(map[k][i][j] == 'S')
{
s[0] = k,s[1] = i,s[2] = j;
}
else if(map[k][i][j] == 'P')
{
e[0] = k,e[1] = i,e[2] = j;
}
}
}
}
bfs();
}
return 0;
}