码迷,mamicode.com
首页 > 其他好文 > 详细

【搜索】POJ2251:Dungeon Master

时间:2018-01-19 19:53:23      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:style   south   lap   color   output   aced   show   ++   and   

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 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

题解:第一次接触bfs也是第一次使用队列,挺简单的,就是将方向改为六个

技术分享图片
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <queue>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 char dt[35][35][35];
 9 int via[35][35][35];
10 int k,n,m,sx,sy,sz,ex,ey,ez;
11 int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
12 
13 struct data
14 {
15     int x,y,z,step;
16 };
17 
18 int check(int x,int y,int z)
19 {
20     if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m)
21         return 1;
22     else if(dt[x][y][z] == #)
23         return 1;
24     else if(via[x][y][z])
25         return 1;
26     return 0;
27 }
28 
29 int bfs()
30 {
31     int i;
32     data now,next;
33     queue<data> Q;
34     now.x = sx,now.y = sy,now.z = sz;
35     now.step = 0;
36     via[sx][sy][sz] = 1;
37     Q.push(now);
38     while(!Q.empty())
39     {
40         now = Q.front();
41         Q.pop();
42         if(now.x == ex && now.y == ey && now.z == ez)
43             return now.step;
44         for(i = 0; i<6; i++)
45         {
46             next = now;
47             next.x =now.x+to[i][0];
48             next.y = now.y+to[i][1];
49             next.z = now.z+to[i][2];
50             if(check(next.x,next.y,next.z))
51                 continue;
52             via[next.x][next.y][next.z] = 1;
53             next.step = now.step+1;
54             Q.push(next);
55         }
56     }
57     return 0;
58 }
59 
60 int main()
61 {
62     int i,j,r;
63     while(scanf("%d%d%d",&k,&n,&m))
64     {
65         if(k==0&&n==0&&m==0)
66         {
67             break;
68         }
69         for(i = 0; i<k; i++)
70         {
71             for(j = 0; j<n; j++)
72             {
73                 scanf("%s",dt[i][j]);
74                 for(r = 0; r<m; r++)
75                 {
76                     if(dt[i][j][r] == S)
77                     {
78                         sx = i,sy = j,sz = r;
79                     }
80                     else if(dt[i][j][r] == E)
81                     {
82                         ex = i,ey = j,ez = r;
83                     }
84                 }
85             }
86         }
87         memset(via,0,sizeof(via));
88         int ans;
89         ans = bfs();
90         if(ans)
91             printf("Escaped in %d minute(s).\n",ans);
92         else
93             cout << "Trapped!" <<endl;
94     }
95 
96     return 0;
97 }
View Code

 

【搜索】POJ2251:Dungeon Master

标签:style   south   lap   color   output   aced   show   ++   and   

原文地址:https://www.cnblogs.com/SoulSecret/p/8318406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!