POJ - 2251 Dungeon Master

POJ - 2251 Dungeon Master


BFS


#include"stdio.h"
#include"queue"
using namespace std;
int i, j, k;
char map[31][31][31];
int dir[6][3]=
{
    {1,0,0},{0,1,0},{0,0,1},
    {-1,0,0},{0,-1,0},{0,0,-1}
};
int si, sj, sk;
int L, R, C;
struct node {
    int x, y, z, step;
}b;
bool check(int x, int y, int z)
{
    if (x > 0 && x <= L && y > 0 && y <= R && z > 0 && z <= C)
        return true;
    else return false;
}
void input()
{
    getchar();
    for (i = 1; i <= L; ++i)
    {
        for (j = 1; j <= R; ++j)
        {
            for (k = 1; k <= C; ++k)
            {
                scanf("%c", &map[i][j][k]);
                if (map[i][j][k] == 'S')
                {
                    b.x = i; b.y = j; b.z = k;
                    b.step = 0;
                }
            }
            getchar();
        }
        getchar();
    }
}
void bfs(node a)
{
    queue<node>q;
    node now, next;
    now.x = a.x; now.y = a.y; now.z = a.z;
    q.push(a);
    while (!q.empty())
    {
        now = q.front();
        q.pop();
        for (i = 0; i < 6; ++i)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            next.z = now.z + dir[i][2];
            next.step = now.step;
            if (check(next.x, next.y, next.z) && (map[next.x][next.y][next.z] == '.'||map[next.x][next.y][next.z]=='E'))
            {
                ++next.step;
                if (map[next.x][next.y][next.z]=='E')
                {
                    /*if (next.step == 1)printf("Escaped in 1 minute.");
                    else*/ printf("Escaped in %d minute(s).", next.step);
                    printf("\n"); return;
                }
                map[next.x][next.y][next.z] = '#';
                q.push(next);
            }
        }
    }
    printf("Trapped!\n");
}
int main()
{
    while (scanf("%d%d%d", &L, &R, &C) && L != 0 && R != 0 && C != 0)
    {
        input();
        bfs(b);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值