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

poj2251(bfs寻找最短路径,三位迷宫)

时间:2018-11-06 20:23:44      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:ini   cap   nes   blank   ems   roc   for   body   you   

题目链接:http://poj.org/problem?id=2251
Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 50755   Accepted: 19053

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!

Source

比较简单,多输入几层迷宫,4个方向变成6个方向就ok了.(我也不知道为什么,实验课偷偷做题被这道sb题卡半天。。。可能是报复吧QAQ)
需要注意的坑点是,
输入顺序,先是z轴,再是xy的二维平面
代码附上:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<queue>
 4 #include<string>
 5 #include<cstring>
 6 #define sca(a) scanf("%d",&a );
 7 #define sca2(a,b) scanf("%d%d",&a,&b );
 8 #define sca3(a,b,c) scanf("%d%d%d",&a,&b,&c);
 9 #define mem(a,b) memset(a,b,sizeof(a));
10 #define FOR0(i,j) for(int i=0;i<=j;++i)
11 #define FOR1(i,j) for(int i=1;i<=j;++i)
12 using namespace std;
13 struct node
14 {
15     int x,y,z,step;
16 };
17 char plat[40][40][40];
18 int vis[40][40][40];
19 int dir[6][3]={0,0,1,0,1,0,1,0,0,-1,0,0,0,-1,0,0,0,-1};
20 int m,n,o;
21 int sx,sy,sz;
22 void ini()
23 {
24     mem(plat,0);
25     mem(vis,0);
26 }
27 int main()
28 {
29     //cout<<sx<<sy<<sz;
30     while(scanf("%d%d%d",&o,&m,&n)&&m+n+o)
31     {
32         ini();
33         for(int i=1;i<=o;i++)
34         {
35             for(int j=1;j<=m;j++)
36             {
37                 scanf("%s", plat[i][j]+1);
38                 for(int k=1;k<=n;k++)
39                 {
40                     if(plat[i][j][k]==S)
41                     {
42                         sx=j;sy=k;sz=i;
43                     }
44                 }
45             }
46         }
47         //cout<<sx<<sy<<sz;
48         queue<node>q;
49         q.push({sx,sy,sz,0});
50         vis[sz][sx][sy]=1;
51         int flag=0;
52         while(!q.empty())
53         {
54             node now=q.front();
55             q.pop();
56             if(plat[now.z][now.x][now.y]==E)
57             {
58                 flag=1;
59                 printf("Escaped in %d minute(s).\n", now.step);
60                 break;
61             }
62             int nex,ney,nez;
63             for(int i=0;i<6;i++)
64             {
65                 nex=now.x+dir[i][0];
66                 ney=now.y+dir[i][1];
67                 nez=now.z+dir[i][2];
68                 if(nex<1||nex>m||ney<1||ney>n||nez<1||nez>o) continue;
69                 if(vis[nez][nex][ney]) continue;
70                 if(plat[nez][nex][ney]==.||plat[nez][nex][ney]==E)
71                 {
72                     //printf("%d %d %d\n",nex,ney,nez );
73                     vis[nez][nex][ney]=1;
74                     q.push({nex,ney,nez,now.step+1});
75                 }
76             }
77         }
78         if(!flag)
79             printf("Trapped!\n");
80 }
81 return 0;
82 
83 }

 

poj2251(bfs寻找最短路径,三位迷宫)

标签:ini   cap   nes   blank   ems   roc   for   body   you   

原文地址:https://www.cnblogs.com/codeoosacm/p/9917568.html

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