POJ2251Dungeon Master(AC)

来源:互联网 发布:通辽数据恢复公司 编辑:程序博客网 时间:2024/06/07 03:07
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 22147 Accepted: 8634

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.

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!
 
感想:这个是三维迷宫的问题,总的来说应该是不难的,自己在细节上有很多错误,主要是6个顺序弄对就好了

 

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>//第一次提交WA,后扩大maxint到100还是不行//第二次提交WA visit[startl][startx][starty] = 1; 这个漏了,然后if ((l1<0)||(l1>=L)||(x1<0)||(x1>=R)||(y1<0)||(y1>=C)) continue这个一开始写的>L也错了应该>=L,//for (i = 0; i < 6;i++)也写错了,原来写的是<5#define MAXINT 100int L = 0;int R = 0;int C = 0;int ans    = 0;int startl = 0;int startx = 0;int starty = 0;int endl   = 0;int endx   = 0;int endy   = 0;int cur    = 0;//三维的int dir[6][3] = { { 0, -1, 0 }, { 0, 1, 0 }, { 0, 0, -1 }, { 0, 0, 1 }, { 1, 0, 0 },{ -1, 0, 0 }};//前面4个是在自己的平面上东南西北,后面2个是在2边的平面上下typedef struct node{int l;int x;int y;int step;}nodes;char map[MAXINT][MAXINT][MAXINT];nodes que[MAXINT*MAXINT*MAXINT];int visit[MAXINT][MAXINT][MAXINT];void init(){int i = 0;int j = 0;int k = 0;int num = 0;for (i = 0; i < MAXINT; i++){for (j = 0; j < MAXINT; j++){for (k = 0; k < MAXINT; k++){map[i][j][k]    = '\0';visit[i][j][k]  = 0;que[num].l      = 0;que[num].x      = 0;que[num].y      = 0;que[num++].step = 0;}}}ans    = 0; cur    = 0;startl = 0;startx = 0;starty = 0;endl = 0;endx = 0;endy = 0;return;}void insert(int l,int x, int y, int step){int i = 0;//因为这个没有什么优先队列之说,所以直接把后面往前移动一步,然后最低一个0位给新的for (i = cur; i > 0;i--){que[i].l    = que[i-1].l;que[i].x    = que[i - 1].x;que[i].y    = que[i - 1].y;que[i].step = que[i - 1].step;}que[0].l    = l;que[0].x    = x;que[0].y    = y;que[0].step = step;return;}void BFS(){int i = 0;que[cur].l      = startl;que[cur].x      = startx;que[cur].y      = starty;que[cur++].step = 0;visit[startl][startx][starty] = 1; //这个一开始漏了while (cur--){int tmpl = que[cur].l;int tmpx = que[cur].x;int tmpy = que[cur].y;int tmps = que[cur].step;if ((endl == tmpl) && (endx == tmpx) && (endy == tmpy)){ans = tmps;return;}for (i = 0; i < 6;i++){int l1 = tmpl + dir[i][0];int x1 = tmpx + dir[i][1];int y1 = tmpy + dir[i][2];if ((l1<0)||(l1>=L)||(x1<0)||(x1>=R)||(y1<0)||(y1>=C)) continue;if ('#' == map[l1][x1][y1]) continue;if (1 == visit[l1][x1][y1]) continue;insert(l1,x1,y1,tmps+1);cur++;visit[l1][x1][y1] = 1;}}return;}int main(){int i = 0;int j = 0;int k = 0;freopen("input.txt","r",stdin);while ((3 == scanf("%d %d %d", &L, &R, &C)) && (0 != L) && (0 != R) && (0 != C)){init();for (i = 0; i < L;i++){for (j = 0; j < R;j++){scanf("%s",&map[i][j]);}//中间是隔了个空行的scanf("");}//找出S和Efor (i = 0; i < MAXINT; i++){for (j = 0; j < MAXINT; j++){for (k = 0; k < MAXINT; k++){if ('S' == map[i][j][k]){startl = i;startx = j;starty = k;}if ('E' == map[i][j][k]){endl = i;endx = j;endy = k;}}}}//BFS();if (0 != ans){printf("Escaped in %d minute(s).\n",ans);}else{printf("Trapped!\n");}}return 0;}


 

0 0
原创粉丝点击