POJ 2251 Dungeon Master

3D迷宫,找从开始点到终点的最短路,挺水的一题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;

struct Point3D
{
    int x;
    int y;
    int z;
};
int x,y,z;
Point3D s,e;
queue<Point3D> q;
char dun[40][40][40];
int mem[40][40][40];
const int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
bool judge(Point3D t)
{
    if (t.x < 0 || t.x >= x || t.y < 0 || t.y >= y || t.z < 0 || t.z >= z)
        return false;
    if (dun[t.x][t.y][t.z] == '#')
        return false;
    return true;
}
void BFS()
{
    int i;
    Point3D tp,ttp;
    while (!q.empty())
        q.pop();
    q.push(s);
    memset(mem,0X3f,sizeof(mem));
    mem[s.x][s.y][s.z]=0;
    while (!q.empty())
    {
        tp=q.front();
        q.pop();
        for (i=0; i<6; i++)
        {
            ttp.x=tp.x+dir[i][0];
            ttp.y=tp.y+dir[i][1];
            ttp.z=tp.z+dir[i][2];
            if (judge(ttp))
            {
                if (mem[ttp.x][ttp.y][ttp.z] > mem[tp.x][tp.y][tp.z]+1)
                {
                    mem[ttp.x][ttp.y][ttp.z]=mem[tp.x][tp.y][tp.z]+1;
                    q.push(ttp);
                }
            }
        }
    }
}
int main()
{
    int t,i,j,k;
    char tc;
    while (1)
    {
        scanf("%d%d%d",&x,&y,&z);
        if (x == 0 && y == 0 && z == 0)
            break;
        getchar();
        for (i=0; i<x; i++)
        {
            for (j=0; j<y; j++)
            {
                for (k=0; k<z; k++)
                {
                    scanf("%c",&dun[i][j][k]);
                    if (dun[i][j][k] == 'S')
                    {
                        s.x=i;
                        s.y=j;
                        s.z=k;
                    }
                    else if (dun[i][j][k] == 'E')
                    {
                        e.x=i;
                        e.y=j;
                        e.z=k;
                    }
                }
                getchar();
            }
            getchar();
        }
        BFS();
        if (mem[e.x][e.y][e.z] == INF)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",mem[e.x][e.y][e.z]);
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值