特别要注意的是,如果时光机对面仍然是时光机..这样是过不去的,题目也没说这样不可以过啊..坑
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 15;
char pic[2][maxn][maxn];
bool book[2][maxn][maxn];
int row, col, T;
struct node
{
int x, y, z, s;
node() {}
node(int xx, int yy, int zz, int ss): x(xx), y(yy), z(zz), s(ss) {}
};
bool bfs(void)
{
int next[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int x = 0, y = 0, z = 0, s = 0;
queue<node> q;
q.push(node(0, 0, 0, 0));
while(!q.empty())
{
node t = q.front();
q.pop();
if(t.s > T) return 0;
if(pic[t.z][t.x][t.y] == 'P') return 1;
for(int i = 0; i < 4; i++)
{
int tx = t.x+next[i][0];
int ty = t.y+next[i][1];
int tz = t.z;
if(tx >= 0 && tx < row && ty >= 0 && ty < col && !book[tz][tx][ty] && pic[tz][tx][ty] != '*')
{
book[tz][tx][ty] = 1;
if(pic[tz][tx][ty] == '#')
{
//时光机对面只能是出口或者是空地
if(pic[!tz][tx][ty] == '.' || pic[!tz][tx][ty] == 'P')
{
book[!tz][tx][ty] = 1;
q.push(node(tx, ty, !tz, t.s+1));
}
}
else q.push(node(tx, ty, tz, t.s+1));
}
}
}
return 0;
}
int main(void)
{
int t;
cin >> t;
while(t--)
{
memset(book, 0, sizeof(book));
scanf("%d%d%d", &row, &col, &T);
for(int i = 0; i < 2; i++)
for(int j = 0; j < row; j++)
for(int k = 0; k < col; k++)
scanf(" %c", &pic[i][j][k]);
puts(bfs() ? "YES" : "NO");
}
return 0;
}
A计划
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18131 Accepted Submission(s): 4599
Problem 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
Source