cdoj1088-王之迷宫 (三维迷宫最短路径)【BFS】

http://acm.uestc.edu.cn/#/problem/show/1088

王之迷宫

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

王被困在了一个3维的迷宫中,他很想逃离这个迷宫回去当学霸,你能帮助他么? 由于王很仁慈,他悄悄地告诉你,本题读入迷宫的每一行时,要用scanf("%s"...) ......

Input

多组测试数据,对于每组测试数据,有三个整数 L,R,C0<l,r,c30)。

L代表迷宫的高度,RC分别代表每一层的行和列。

接下来是LR×C的矩阵,矩阵包含4种字符(S,E,.,#),S代表王的初始位置,E代表出口,#代表障碍。.代表能通过的地方。

每一层之后有一个空行。

L=R=C=0时,输入中断。

Output

如果可以逃离迷宫,按下列格式输出最短时间:

Escaped in x minute(s). (x表示逃离迷宫的最短时间, 走一步花费一昏钟)

否则,输出:

Trapped!

Sample input and output

Sample InputSample Output
3 4 5
S....
.###.
.##..
###.#

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

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

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

0 0 0
Escaped in 11 minute(s).
Trapped!

 

 

题解:三维迷宫,很简单的bfs。一开始wa了一发,后来发现是因为oj数据不规范,不能用getchar去消除空白符,得用%s过滤空白符。一方面有些抱怨oj数据坑爹,一方面总结教训,要有勇气敢于怀疑平台数据的正确性,改善自己程序的健壮性。

代码:

 1 #include <fstream>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <cstdlib>
 6 #include <cstring>
 7 #include <algorithm>
 8 #include <queue>
 9 
10 using namespace std;
11 
12 struct node{
13     int x,y,z,step;
14 }tmp;
15 
16 const int N=35;
17 int l,r,c,x1,y1_,z1;
18 queue<node> pq;
19 char a[N][N][N];
20 bool b[N][N][N];
21 int co[3][6]={{0,0,0,0,1,-1},{0,0,1,-1,0,0},{1,-1,0,0,0,0}};
22 
23 inline bool check(int x,int y,int z);
24 int bfs();
25 
26 int main()
27 {
28     //freopen("D:\\input.in","r",stdin);
29     //freopen("D:\\output.out","w",stdout);
30     while(scanf("%d%d%d",&l,&r,&c)&&(l|r|c)){
31         bool bo=1;
32         for(int i=0;i<l;i++){
33             for(int j=0;j<r;j++){
34                 scanf("%s",a[i][j]);
35                 if(bo)
36                     for(int z=0;z<c;z++){
37                         if(a[i][j][z]=='S'){
38                             x1=i,y1_=j,z1=z;
39                             bo=0;
40                             break;
41                         }
42                     }
43             }
44         }
45         int ans=bfs();
46         if(ans==-1) puts("Trapped!");
47         else    printf("Escaped in %d minute(s).\n",ans);
48     }
49     return 0;
50 }
51 inline bool check(int x,int y,int z){
52     return x>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c&&a[x][y][z]!='#'&&b[x][y][z]==0;
53 }
54 int bfs(){
55     memset(b,0,sizeof(b));
56     while(!pq.empty()) pq.pop();
57     tmp.step=0;
58     tmp.x=x1;
59     tmp.y=y1_;
60     tmp.z=z1;
61     pq.push(tmp);
62     b[x1][y1_][z1]=1;
63     while(!pq.empty()){
64         tmp=pq.front();
65         pq.pop();
66         int x=tmp.x,y=tmp.y,z=tmp.z;
67         int tx,ty,tz,ts=tmp.step+1;
68         for(int i=0;i<6;i++){
69             tx=x+co[0][i];
70             ty=y+co[1][i];
71             tz=z+co[2][i];
72             if(check(tx,ty,tz)){
73                 b[tx][ty][tz]=1;
74                 if(a[tx][ty][tz]=='.'){
75                     tmp.x=tx,tmp.y=ty,tmp.z=tz,tmp.step=ts;
76                     pq.push(tmp);
77                 }else{
78                     return ts;
79                 }
80             }
81         }
82     }
83     return -1;
84 }

转载于:https://www.cnblogs.com/jiu0821/p/4557704.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值