ZOJ Problem Set - 1940 Dungeon Master

BFS

简单的!!!!

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node
{
    int l;
    int r;
    int c;
    int t;
};
char ch[31][31][31];
queue<Node> q;
int main()
{
    int l,r,c,i,j,k,x,y,z,t;
    Node tmp,tmp1,end;
    bool flag ;
    //freopen("a.txt","r",stdin);
    while(scanf("%d%d%d",&l,&r,&c)!=EOF)
    {
        if(l ==0&&r==0&&c==0) break;
        flag = 0;
        
        for(i = 0;i < l;i ++)
            for(j = 0;j < r;j ++)
                for(k = 0;k < c;k ++)
                {
                    cin>>ch[i][j][k];
                    if(ch[i][j][k] == 'S')
                    {
                        tmp.l = i;
                        tmp.r = j;
                        tmp.c = k;
                        tmp.t = 0;
                        q.push(tmp);
                        ch[i][j][k] = '#';
                    }
                    if(ch[i][j][k] == 'E')
                    {
                        end.l = i;
                        end.r = j;
                        end.c = k;
                    }

                }
        
        int dir[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
        while(!q.empty())
        {
            tmp = q.front();
            q.pop();
            if(tmp.l==end.l&&tmp.r == end.r&&tmp.c == end.c) {flag = 1;
            printf("Escaped in %d minute(s).\n",tmp.t);
            break;}
            else
            {
                for(i = 0;i < 6;i ++)
                {
                    x = tmp.l + dir[i][0];
                    y = tmp.r + dir[i][1];
                    z = tmp.c + dir[i][2];
                    t = tmp.t + 1;
                    if(x>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c&&ch[x][y][z]!='#'){
                        tmp1.l = x;
                        tmp1.r = y;
                        tmp1.c = z;
                        tmp1.t = t;
                        q.push(tmp1);
                        ch[tmp1.l][tmp1.r][tmp1.c] = '#';
                    }
                }
            }
            
        }
        if(flag == 0) 
        printf("Trapped!\n");
        while(!q.empty()) q.pop();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值