• POJ 2251 Dungeon Master(三维空间bfs)


    题意:三维空间求最短路,可前后左右上下移动。

    分析:开三维数组即可。

    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int MAXN = 30 + 10;
    char pic[MAXN][MAXN][MAXN];
    bool vis[MAXN][MAXN][MAXN];
    int sx, sy, sz;
    int dr[] = {0, 0, 1, -1, 0, 0};
    int dc[] = {1, -1, 0, 0, 0, 0};
    int dz[] = {0, 0, 0, 0, 1, -1};
    int L, R, C;
    bool judge(int x, int y, int z){
        return x >= 0 && x < R && y >= 0 && y < C && z >= 0 && z < L;
    }
    int bfs(){
        queue<int> x, y, z, step;
        x.push(sx), y.push(sy), z.push(sz), step.push(0);
        vis[sz][sx][sy] = true;
        while(!x.empty()){
            int tmpx = x.front(); x.pop();
            int tmpy = y.front(); y.pop();
            int tmpz = z.front(); z.pop();
            int tmpstep = step.front(); step.pop();
            for(int i = 0; i < 6; ++i){
                int tx = tmpx + dr[i];
                int ty = tmpy + dc[i];
                int tz = tmpz + dz[i];
                if(judge(tx, ty, tz)){
                    if(pic[tz][tx][ty] == 'E') return tmpstep + 1;
                    if(pic[tz][tx][ty] != '#' && !vis[tz][tx][ty]){
                        vis[tz][tx][ty] = true;
                        x.push(tx);
                        y.push(ty);
                        z.push(tz);
                        step.push(tmpstep + 1);
                    }
                }
            }
        }
        return -1;
    }
    int main(){
        while(scanf("%d%d%d", &L, &R, &C) == 3){
            if(!L && !R && !C) return 0;
            memset(vis, false, sizeof vis);
            for(int i = 0; i < L; ++i){
                for(int j = 0; j < R; ++j){
                    scanf("%s", pic[i][j]);
                    for(int k = 0; k < C; ++k){
                        if(pic[i][j][k] == 'S'){
                            sz = i, sx = j, sy = k;
                        }
                    }
                }
            }
            int ans = bfs();
            if(ans == -1) printf("Trapped!
    ");
            else printf("Escaped in %d minute(s).
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    每日一题20180325
    Linux下MySQL表名区分大小写
    CentOS删除编译安装的Python3
    HTTPS配置
    测试 js 方法运行时间(转)
    使用dbutils进行批处理
    oracle生成主键
    JDBC学习笔记(10)——调用函数&存储过程(转)
    easyui Draggable
    blob
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6874889.html
Copyright © 2020-2023  润新知