zoukankan      html  css  js  c++  java
  • POJ2251 Dungeon Master(bfs)

    题目链接

    题目大意:

    三维迷宫,搜索从s到e的最小步骤数。

    分析:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    
    const int maxn = 35;
    
    int dx[] = {0, 0, -1, 1, 0, 0};
    int dy[] = {0, 0, 0, 0, -1, 1};
    int dz[] = {-1, 1, 0, 0, 0, 0};
    
    char G[maxn][maxn][maxn];
    bool vis[maxn][maxn][maxn];
    int r, l, c;
    
    struct Pos {
        int x, y, z;
        int step;
    };
    
    int BFS(int x0, int y0, int z0) {
        queue<Pos> Q;
    
        Q.push((Pos){x0, y0, z0, 0});
    
        while(!Q.empty()) {
            Pos e = Q.front(); Q.pop();
            int x=e.x, y=e.y, z=e.z, s=e.step;
    
            if(G[z][x][y] == 'E') return s;
    
            for(int d=0; d<6; d++) {
                int nx, ny, nz;
                nx = x+dx[d];
                ny = y+dy[d];
                nz = z+dz[d];
    
                if(nx < 0 || ny < 0 || nz < 0) continue;
                if(nx >= r || ny >= c || nz >= l) continue;
                if(vis[nz][nx][ny]) continue;
                if(G[nz][nx][ny] == '#') continue;
    
                vis[nz][nx][ny] = true;
    
                Q.push((Pos){nx, ny, nz, s+1});
            }
        }
    
        return -1;
    }
    
    int main() {
        int x0, y0, z0;
    
        while(scanf("%d%d%d", &l, &r, &c) == 3) {
            if(l == 0 && r == 0 && c == 0) break;
    
            for(int i=0; i<l; i++) {
                for(int j=0; j<r; j++) {
                    scanf("%s", G[i][j]);
                }
            }
    
            for(int i=0; i<l; i++) {
                for(int j=0; j<r; j++) {
                    for(int k=0; k<c; k++) {
                        if(G[i][j][k] == 'S'){
                            z0 = i; x0=j; y0=k;
                        }
                    }
                }
            }
    
            memset(vis, false, sizeof(vis));
    
            int res = BFS(x0, y0, z0);
            
            if(res == -1) {
                printf("Trapped!
    ");
            }
            else
                printf("Escaped in %d minute(s).
    ", res);
        }
    
        return 0;
    }
  • 相关阅读:
    逆向学习-内嵌补丁(洞穴代码)
    ubuntu下创建ftp用户,该用户只访问特定目录下的内容
    Ubuntu 14.04 FTP服务器--vsftpd的安装和配置
    Hdu 4223 Dynamic Programming?
    Hdu 3873 Invade the Mars
    Hdu 2025 查找最大元素
    Hdu 1520 Anniversary party
    Hdu 4283 You Are the One
    HTTP协议
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3228186.html
Copyright © 2011-2022 走看看