BFS_poj 2251 Dungeon Master

题目大意:
给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径

移动方向可以是上,下,左,右,前,后,六个方向

每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的

/*
Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!
*/

#include<cstdio>
#include<queue>
#include<cstring>
#include<cstdlib>

using namespace std;

typedef struct point{
    int x, y, z, r;
}point;

char str[31][31][31];

int arr[6][3] = {{0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0},{0,0,1}, {0,0,-1}};

int bfs(point start, int l, int n, int m){
    queue<point> que;
    que.push(start);
        point goal = {0,0,0,0}, p1 = {0,0,0,0};
        while(!que.empty()){
            p1 = que.front();
            que.pop();
            for(int k = 0; k < 6; k++){
                goal.x = p1.x + arr[k][0];
                goal.y = p1.y + arr[k][1];
                goal.z = p1.z + arr[k][2];
                goal.r = p1.r + 1;
                if(str[goal.x][goal.y][goal.z] == 'E'){
                    return goal.r;
                }
                if(goal.x >= 0 && goal.x < l && goal.y >= 0 && goal.y < n && goal.z >= 0
                   && goal.z < m && str[goal.x][goal.y][goal.z] != '#'){
                    str[goal.x][goal.y][goal.z] = '#';
                    que.push(goal);
                }
            }
        }
        return -1;
}
int main(){
    int l, n, m, i, j;
    while(scanf("%d%d%d", &l, &n, &m) && (l || m || n)){
        getchar();
        point start = {0,0,0,0};
        int flag = 0;
        for(int i = 0; i < l ; i++){
            for(int j = 0; j < n; j++){
                gets(str[i][j]);
                if(!flag){
                    for(int k = 0; k < m; k++){
                        if(str[i][j][k] == 'S'){
                            start.x = i;
                            start.y = j;
                            start.z  = k;
                            start.r = 0;
                            flag = 1;
                        }
                    }
                }
            }
            getchar();
        }
        int re = bfs(start, l, n, m);
        if(re != -1) printf("Escaped in %d minute(s).\n", re);
        else printf("Trapped!\n");
    }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值