Uva 532 - Dungeon Master(三维迷宫)
Dungeon Master |
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 Specification
The input file 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.
Output Specification
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!
#include<stdio.h> #include<malloc.h> #define MAXN 32 typedef struct Queue{ int value; int x, y, z; struct Queue *next; }Queue; int direction[6][3] = {{-1, 0, 0}, {0, 0, -1}, {0, 1, 0}, {0, 0, 1}, {0, -1, 0}, {1, 0, 0}}; int is_value(int i, int j, int z, int l, int r, int c) { return (i>=0 && i<l && j>=0 && j<r && z>=0 && z<c); } /* void Print(int (*maze)[MAXN][MAXN], int l, int r, int c) { for(int i=0; i<l; ++i) { for(int j=0; j<r; ++j) { for(int t=0; t<c; ++t) printf("%2d ", maze[i][j][t]); printf("\n"); } printf("\n"); } return; } */ void Traverse(int (*maze)[MAXN][MAXN], int x, int y, int z, int dx, int dy, int dz, int l, int r, int c, int step) { int k, i, j, t; Queue *str = NULL, *stp = NULL, *stf = NULL, *tail = NULL; str = (Queue*)malloc(sizeof(Queue)); str->value = 0, str->x = x, str->y = y, str->z = z, str->next = NULL; tail = str; while(str != NULL) { for(k=0; k<6; ++k) { i = str->x + direction[k][0]; j = str->y + direction[k][1]; t = str->z + direction[k][2]; if(is_value(i, j ,t, l, r, c) && maze[i][j][t] != -3 && maze[i][j][t] < 0) { /* if(maze[i][j][t] == -2 || (maze[i][j][t] > str->value)) */ { stf = (Queue*)malloc(sizeof(Queue)); maze[i][j][t] = stf->value = str->value+1; stf->x = i, stf->y = j, stf->z = t; stf->next = NULL; tail->next = stf; tail = stf; if(i == dx && j == dy && t == dz) return; } } } stp = str->next; free(str); str = stp; } return; } int main() { /* freopen("input.txt", "r", stdin); */ int l, r, c, sx, sy, sz, dx, dy, dz, i, j, t, step; int maze[MAXN][MAXN][MAXN]; char temp; while(scanf("%d%d%d", &l, &r, &c) != EOF) { getchar(); if(!l && !r && !c) break; for(i=0; i<l; getchar(), ++i) for(j=0; j<r; getchar(), ++j) for(t=0; t<c; ++t) { scanf("%c", &temp); switch(temp) { case '#' : maze[i][j][t] = -3; break; case '.' : maze[i][j][t] = -2; break; case 'S' : maze[i][j][t] = 0; sx=i; sy=j; sz=t; break; case 'E' : maze[i][j][t] = -2; dx=i; dy=j; dz=t; break; } } step = 0; Traverse(maze, sx, sy, sz, dx, dy, dz, l, r, c, step); if(maze[dx][dy][dz] != -2) printf("Escaped in %d minute(s).\n", maze[dx][dy][dz]); else printf("Trapped!\n"); } return 0; }
解题思路:
三维的情况本来是不难的,但对于我这样习惯都用DFS去做的人就有点担忧了,Limit time 竟然可以达到4次!无奈之下才找师兄,然后他叫我是使用BFS,我就纳闷两者之间的区别了,上网搜搜才知道BFS找到的不一定是最短路径(我知道这点,所以在深度搜索的时候还特意的处理了一下,但还是超时了),后来知道了BFS的优点——一层一层的搜索找到的一定的是最短的,所以改进了代码,在遇到出口时,直接就退出BFS了,这时才AC了,来之不易!
更多内容请关注个人微信公众号 物役记 (微信号:materialchains)