POJ2251 Dungeon Master

题目:http://poj.org/problem?id=2251

一个三维地牢,L为层数,R为每层的行数,C为每层的列数,S为起点,E为终点,
‘#’为障碍,‘.’表示可以通过,可以上下左右前后行走,每走一步耗时一分钟,
问能否从起点S走到终点E,若能,则输出最少花费的时间,若不能,则输出“Trapped!”。

BFS

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 32;
int L, R, C;
char Map[MAXN][MAXN][MAXN]; // L, R, C
int book[MAXN][MAXN][MAXN];
int dir[6][3] = {1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};
int min_steps = INF;
struct node
{
    int z, x, y, step;
}s, e;  // s为start,起点; e是end,终点
void read_map()
{
    for(int i = 0; i < L; i++)
    {
        for(int r = 0; r < R; r++)
        {
            for(int c = 0; c < C; c++)
            {
                cin >> Map[i][r][c];
                if(Map[i][r][c] == 'S') // 起点
                {
                    s.z = i; s.x = r; s.y = c;
                }
                else if(Map[i][r][c] == 'E')  // 终点
                {
                   e.z = i; e.x = r; e.y = c;
                }
            }
        }
    }
}
void bfs()
{
    queue<node> q;
    q.push(s);
    while(!q.empty())
    {
        node now = q.front(); q.pop();
        if(now.z == e.z && now.x == e.x && now.y == e.y)
        {
            min_steps = min(min_steps, now.step);
            return ;
        }

        for(int i = 0; i < 6; i++)
        {
            node nxt;
            nxt.z = now.z + dir[i][0];
            nxt.x = now.x + dir[i][1];
            nxt.y = now.y + dir[i][2];

            if(nxt.z >= 0 && nxt.z < L
               && nxt.x >= 0 && nxt.x < R
               && nxt.y >= 0 && nxt.y < C
               && Map[nxt.z][nxt.x][nxt.y] != '#')
            {
                if(!book[nxt.z][nxt.x][nxt.y])
                {
                    nxt.step = now.step + 1;
                    book[nxt.z][nxt.x][nxt.y] = 1;
                    q.push(nxt);
                }
            }
        }
    }

}
int main()
{
    while(~scanf("%d %d %d", &L, &R, &C) && (L + R + C))
    {
        min_steps = INF;
        memset(Map, 0, sizeof(Map));
        memset(book, 0, sizeof(book));

        read_map();

        s.step = 0;
        book[s.z][s.x][s.y] = 1;

        bfs();
        if(min_steps == INF) cout << "Trapped!" << endl;
        else printf("Escaped in %d minute(s).\n", min_steps);
    }
    return 0;
}

DFS超时

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 32;
int L, R, C;
char Map[MAXN][MAXN][MAXN]; // L, R, C
int book[MAXN][MAXN][MAXN];
int sL, sR, sC;
int eL, eR, eC;
int dir[6][3] = {1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};
int min_steps = INF;
void read_map()
{
    for(int i = 0; i < L; i++)
    {
        for(int r = 0; r < R; r++)
        {
            for(int c = 0; c < C; c++)
            {
                cin >> Map[i][r][c];
                if(Map[i][r][c] == 'S')
                {
                    sL = i; sR = r; sC = c;
                }
                else if(Map[i][r][c] == 'E')
                {
                    eL = i; eR = r; eC = c;
                }
            }
        }
    }
}
void dfs(int nz, int nx, int ny, int steps)
{
    if(nz == eL && nx == eR && ny == eC)
    {
        if(steps < min_steps)
        {
            min_steps = steps;
            return ;
        }
    }

    for(int i = 0; i < 6; i++)
    {
        int next_z = nz + dir[i][0];
        int next_x = nx + dir[i][1];
        int next_y = ny + dir[i][2];

        if(next_z >= 0 && next_z < L
           && next_x >= 0 && next_x <= R
           && next_y >= 0 && next_y <= C
           && Map[next_z][next_x][next_y] != '#')
        {
            if(!book[next_z][next_x][next_y])
            {
                book[next_z][next_x][next_y] = 1;
                dfs(next_z, next_x,next_y, steps + 1);
                book[next_z][next_x][next_y] = 0;
            }
        }
    }
}
int main()
{
    while(~scanf("%d %d %d", &L, &R, &C) && (L + R + C))
    {
        min_steps = INF;
        memset(Map, 0, sizeof(Map));
        memset(book, 0, sizeof(book));

        read_map();
        book[sL][sR][sC] = 1;
        dfs(sL, sR, sC, 0);
        if(min_steps == INF) cout << "Trapped!" << endl;
        else printf("Escaped in %d minute(s).\n", min_steps);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值