Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 55224 | Accepted: 20493 |
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped! 思路:看了网上很多人用的都是三维的bfs,但是此题可以用二维解决。 向上或向下走时,只要令x坐标减去或加上R即可,要注意的就是往东西南北方向走时不能越层,而只能在同一层走。
输入中的换行可以无视。 当时做的时候找错找了好久,最后发现是for循环里面的t.y写成了t.x,我晕~ ---->>>> 以后写代码时一定要细心(ノ▼Д▼)ノ
#include<iostream>
#include<cstring> //使用memset必须加此头文件
#include<stdio.h> //使用printf必须加此头文件
#include<queue> using namespace std; int L, R, C;
int minute = ;
char maze[][];
int vis[][];
int dx[] = { ,-,, }; //南北方向
int dy[] = { ,,,- }; //东西方向 struct node
{
int x, y;
int step;
}s; void bfs(int x, int y)
{
s.x = x;
s.y = y;
s.step = ;
vis[x][y] = ;
queue<node> Q;
Q.push(s); node t;
while (!Q.empty())
{
t = Q.front();
Q.pop(); if (maze[t.x][t.y] == 'E')
{
printf("Escaped in %d minute(s).\n", t.step);
return;
} // k表示此坐标所在的层数,因为如果只是往东西南北方向走的话,只能在同一层
int k = t.x / R + ; for (int i = ; i < ; ++i)
{
int xx = t.x + dx[i];
int yy = t.y + dy[i]; //注意同一层中的坐标判断条件
if ((xx >= ((k - )*R)) && (xx < (k*R)) && yy >= & yy < C && maze[xx][yy] != '#' && !vis[xx][yy])
{
vis[xx][yy] = ;
s.x = xx;
s.y = yy;
s.step = t.step + ;
Q.push(s);
}
} //跳入下一层
int x1 = t.x + R;
int y1 = t.y;
if (x1 < L*R && maze[x1][y1] != '#' && !vis[x1][y1])
{
vis[x1][y1] = ;
s.x = x1;
s.y = y1;
s.step = t.step + ;
Q.push(s);
} //跳入上一层
int x2 = t.x - R;
int y2 = t.y;
if (x2 >= && maze[x2][y2] != '#' && !vis[x2][y2])
{
vis[x2][y2] = ;
s.x = x2;
s.y = y2;
s.step = t.step + ;
Q.push(s);
}
} cout << "Trapped!" << endl; } int main()
{
int start_x, start_y; //记录起点坐标
while (cin >> L >> R >> C)
{
if (L == && R == && C == )
break; for (int i = ; i < L*R; ++i)
for (int j = ; j < C; ++j)
{
cin >> maze[i][j]; //迷宫下标从0开始存储
if (maze[i][j] == 'S')
{
start_x = i;
start_y = j;
}
} memset(vis, , sizeof(vis));
bfs(start_x, start_y); }
return ;
}