POJ 2251 Dungeon Master(BFS+数据读取)

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21116 Accepted: 8210

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!

这题让我改了好久。。。。原因很简单因为,没有分清r,c分别表示的什么但是题目都说 了,所以这两天脑子进水了。。。。- -!

也没啥好说的,就是让求3维空间S到E的最短距离。很显然BFS。

主要是构建人的行走方向,按照一定的顺序来行走。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
struct node
{
    int a,b,ceng,ans;
};
int l,r,c;
char Map[40][40][40];
bool vis[40][40][40],bj;
int mo[6][3]= {{-1,0,0},{1,0,0},{0,0,1},{0,-1,0},{0,0,-1},{0,1,0}};
void bfs(int ce,int s1,int s2)
{
    queue<node>q;
    while(!q.empty())
     q.pop();
    memset(vis,false,sizeof(vis));
    node f1,f2;
    f1.ceng=ce;f1.a=s1;f1.b=s2;
    f1.ans=0;
    vis[ce][s1][s2]=true;
    q.push(f1);
    while(!q.empty())
    {
        f1=q.front();
        q.pop();
        if(Map[f1.ceng][f1.a][f1.b]=='E')
        {
            bj=true;
            printf("Escaped in %d minute(s).\n",f1.ans);
            return ;
        }
        for(int i=0;i<6;i++)
        {
            f2=f1;
            f2.ceng+=mo[i][0];
            f2.a+=mo[i][1];
            f2.b+=mo[i][2];
            if(f2.ceng>=0&&f2.ceng<l&&f2.a>=0&&f2.a<r&&f2.b>=0&&f2.b<c&&Map[f2.ceng][f2.a][f2.b]!='#'&&!vis[f2.ceng][f2.a][f2.b])
            {
                f2.ans++;
                vis[f2.ceng][f2.a][f2.b]=true;
                q.push(f2);
            }
        }
    }
}
int main()
{
    int i,j,k;
    int ce,s1,s2;
    while(~scanf("%d%d%d",&l,&r,&c))
    {
        bj=false;
        if(l==0&&r==0&&c==0)
            break;
       for(i=0;i<l;i++)//并且一定要注意输入的问题
        {
            for(j=0;j<r;j++)
            {
                scanf("%s",Map[i][j]);
            }
        }
        for(i=0;i<l;i++)
        {
            for(j=0;j<r;j++)
            {
                for(k=0;k<c;k++)
                {
                    if(Map[i][j][k]=='S')
                    {
                        ce=i;
                        s1=j;
                        s2=k;
                    }
                }
            }
        }
        bfs(ce,s1,s2);
        if(!bj)
        puts("Trapped!");
    }
    return 0;
}
需要强调一点,scanf("%s",str)在遇到'\n'(回车)或' '(空格)时输入结束,但'\n'(回车)或' '(空格)停留在出入缓冲区,如处理不慎会影响下面的输入;gets(str)遇到'\n'(回车)时输入结束,但'\n'(回车)已被替换为'\0',存储于字符串中,输入缓冲中没有遗留的'\n'(回车),不会影响后续的输入。若用gets输入的话前后两边的getchar()必不可少,前边的在输入整数的getchar(),肯定不能少。至于在后边的getchar()为什么要有,因为gets()
能够将所有的字符读取进来如上所述将'\n'代替为‘\0’储存在串中因为串的大小固定,所以必有字符串没有读入,串中。而%s没这么随便什么都读取。
        getchar();
       for(i=0;i<l;i++)
        {
            for(j=0;j<r;j++)
            {
                gets(Map[i][j]);
            }
            getchar();
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值