POJ - 2251 Dungeon Master(三维bfs)

22 篇文章 0 订阅
16 篇文章 0 订阅

Dungeon Master

题意:给你一个三维的图,让你找到从S到E最短的路径。
做法:直接套bfs模板,只不过要把数组改为三维的。
代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
const int N = 30 + 10;
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};			//方向数组,6个方向

char a[N][N][N];		
int vis[N][N][N];
int l, r, c;
int L, R, C;
int ans;
struct node{
    int x;
    int y;
    int z;
    int step;
    node(int x, int y, int z, int step):x(x),y(y),z(z),step(step){}
};
queue<node> s;
bool f(int x, int y, int z)
{
    if(x < 0 || x == l || y < 0 || y == r || z < 0 || z == c ){
        return false;
    }
    return true;
}
int bfs()
{
    while(!s.empty()){
        node cnt = s.front();
        s.pop();
        if (a[cnt.x][cnt.y][cnt.z] == 'E')
            return cnt.step;
        for (int i = 0; i < 6; i++){
            int tx = cnt.x + dir[i][0];
            int ty = cnt.y + dir[i][1];
            int tz = cnt.z + dir[i][2];
            if (f(tx, ty, tz) && !vis[tx][ty][tz] && a[tx][ty][tz] != '#'){
                vis[tx][ty][tz] = 1;
                s.push(node(tx, ty, tz, cnt.step + 1));
            }
        }
    }
    return 0;
}
int main()
{
    while(cin >> l >> r >> c){
        if (l == 0)
            break;
        for (int i = 0; i < l; i++){
            for (int j = 0; j < r; j++){
                for (int k = 0; k < c; k++){
                    cin >> a[i][j][k];
                    if (a[i][j][k] == 'S'){
                        L = i;
                        R = j;
                        C = k;
                    }
                }
            }
            char cc = getchar();
        }
        ans = 0;
        while(!s.empty()){
            s.pop();
        }
        memset(vis, 0, sizeof(vis));
        s.push(node(L, R, C, 0));
        ans = bfs();
        if (ans == 0){
            printf("Trapped!\n");
        }else{
            printf("Escaped in %d minute(s).\n", ans);
        }
    }
    return 0;
}


// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值