POJ2251 Dungeon Master 【BFS】

74 篇文章 0 订阅
23 篇文章 0 订阅

题目链接:http://poj.org/problem?id=2251
题意:给你一个三维迷宫,S是起点,E是终点,#不可走
解析:直接开个三维数组,BFS就好

#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int inf = 0x7fffffff;
int L,R,C;
int vis[50][50][50];
char mapple[50][50][50];
int ans;
int dx[] = {0,1,-1,0,0,0};
int dy[] = {1,0,0,-1,0,0};
int dz[] = {0,0,0,0,1,-1};
int sx,sy,sz;
struct node
{
    int x,y,z;
    int step;
    node() {}
    node(int _x,int _y,int _z,int _step)
    {
        x = _x;
        y = _y;
        z = _z;
        step = _step;
    }
};
int bfs()
{
    ans = inf;
    memset(vis,0,sizeof(vis));
    queue<node> q;
    q.push(node(sx,sy,sz,0));
    vis[sx][sy][sz] = 1;
    while(!q.empty())
    {
        node now = q.front();
        q.pop();
        if(mapple[now.x][now.y][now.z]=='E')
            return now.step;
        for(int i=0;i<6;i++)
        {
            int tx = now.x+dx[i];
            int ty = now.y+dy[i];
            int tz = now.z+dz[i];
            if(tx<0||tx>=L || ty<0||ty>=R || tz<0 || tz>=C)
                continue;
            if(mapple[tx][ty][tz]=='#' || vis[tx][ty][tz])
                continue;
            vis[tx][ty][tz] = 1;
            q.push(node(tx,ty,tz,now.step+1));
        }
    }
    return inf;
}
int main(void)
{
    while(~scanf("%d %d %d",&L,&R,&C))
    {
        if(L==0&&R==0&&C==0)
            break;
        for(int i=0;i<L;i++)
        {
            for(int j=0;j<R;j++)
            {
                scanf("%s",mapple[i][j]);
                for(int k=0;k<C;k++)
                {
                    if(mapple[i][j][k]=='S')
                    {
                        sx = i;
                        sy = j;
                        sz = k;
                    }
                }
            }
        }
        ans = bfs();
        if(ans==inf)
            puts("Trapped!");
        else
            printf("Escaped in %d minute(s).\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值