A计划
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19961 Accepted Submission(s): 5065
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
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct Node
{
int s_row, s_col, s_floor, s_time; //当前骑士的状态,依次为骑士位置的行数、列数、层数、所花时间
Node() { s_row = s_col = s_floor = s_time = 0; }
};
queue<Node> q;
int m, n, t; //迷宫行数、列数、规定时间
char maze[2][12][12]; //存放迷宫
int visit[2][12][12]; //记录迷宫中的点是否被访问过,未访问为0,访问过为1
void bfs()
{
while (!q.empty())
{
Node frontNode = q.front();
q.pop();
for (int i = -1;i < 2;i++)
for (int j = -1;j < 2;j++)
{
if (abs(i) ^ abs(j))
{
int row = frontNode.s_row + i;
int col = frontNode.s_col + j;
int floor = frontNode.s_floor;
if (row < n&&row >= 0 && col < m&&col >= 0 && maze[floor][row][col] != '*' && !visit[floor][row][col]) //可移动的条件
{
if (maze[floor][row][col] == '#') //时空传输机
{
visit[floor][row][col] = 1; //标记当前位置已走过
floor = !floor; //0层变1层,1层变0层
}
Node node;
node.s_row = row;
node.s_col = col;
node.s_floor = floor;
node.s_time = frontNode.s_time + 1;
if (maze[floor][row][col] == 'P' && node.s_time<=t) //找到了公主
{
printf("YES\n");
return;
}
visit[node.s_floor][node.s_row][node.s_col] = 1; //标记当前位置已走过
q.push(node); //入队
}
}
}
}
printf("NO\n");
}
int main()
{
//freopen("test.txt", "r", stdin);
int c;
cin >> c;
while (c--)
{
while (!q.empty())
q.pop();
memset(maze, '\0', sizeof(maze));
memset(visit, 0, sizeof(visit));
Node start;
scanf("%d%d%d", &n, &m, &t);
for (int i = 0;i < n;i++)
{
getchar();
for (int j = 0;j < m;j++)
{
scanf("%c", &maze[0][i][j]);
if (maze[0][i][j] == 'S')
{
start.s_row = i;
start.s_col = j;
start.s_floor = 0;
}
}
}
getchar();
for (int i = 0;i < n;i++)
{
getchar();
for (int j = 0;j < m;j++)
{
scanf("%c", &maze[1][i][j]);
if (maze[1][i][j] == 'S')
{
start.s_row = i;
start.s_col = j;
start.s_floor = 1;
}
}
}
for (int i = 0;i < n;i++) //这样处理防止骑士经时空机传输后被撞死,简化了bfs()函数里的判断条件
for (int j = 0;j < n;j++)
{
if (maze[0][i][j] == '#' && maze[1][i][j]=='#')
maze[0][i][j] = maze[1][i][j] = '*';
if (maze[0][i][j] == '#' && maze[1][i][j] =='*')
maze[0][i][j] = maze[1][i][j] = '*';
if (maze[0][i][j] == '*' && maze[1][i][j] == '#')
maze[0][i][j] = maze[1][i][j] = '*';
}
q.push(start);
visit[start.s_floor][start.s_row][start.s_col] = 1;
bfs();
}
return 0;
}