zoukankan      html  css  js  c++  java
  • POJ 2251 Dungeon Master (BFS)


    **链接 : ** Here!

    **思路 : ** 三维的搜索, 只需要注意输入的图$G$, 第一为是 $z$ 即可. 剩下的就是普通的搜索, 只不过多加了一维而已...


    /*************************************************************************
    	> File Name: E.cpp
    	> Author: 
    	> Mail: 
    	> Created Time: 2017年11月26日 星期日 10时51分05秒
     ************************************************************************/
    
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <queue>
    using namespace std;
    
    #define MAX_N 40
    int L, R, C;
    int dx[6] = {0, 0, -1, 1, 0, 0};
    int dy[6] = {-1, 1, 0, 0, 0, 0};
    int dz[6] = {0, 0, 0, 0, 1, -1};
    int vis[MAX_N][MAX_N][MAX_N];
    char G[MAX_N][MAX_N][MAX_N];
    
    struct Point {
        Point() {}
        Point(int x, int y, int z, int step) : x(x), y(y), z(z), step(step) {}
        int x, y, z;
        int step;
    };
    Point st, ed;
    
    void read() {
        for (int k = 0 ; k < L ; ++k) {
            getchar();
            for (int i = 0 ; i < R ; ++i) {
                scanf("%s", G[k][i]);
            }
        }
        for (int k = 0 ; k < L ; ++k) {
            for (int i = 0 ; i < R ; ++i) {
                for (int j = 0 ; j < C ; ++j) {
                    if (G[k][i][j] == 'S') {
                        st.z = k; st.x = i; st.y = j;
                        st.step = 0;
                    }
                    if (G[k][i][j] == 'E') {
                        ed.z = k; ed.x = i; ed.y = j;
                        ed.step = 0;
                    }
                }
            }
        }
    }
    
    int check(Point pt) {
        if (pt.x < 0 || pt.x >= R || pt.y < 0 || pt.y >= C || pt.z < 0 || pt.z >= L) return 0;
        if (vis[pt.x][pt.y][pt.z]) return 0;
        if (G[pt.z][pt.x][pt.y] == '#') return 0;
        return 1;
    }
    
    int BFS() {
        queue<Point> que;
        vis[st.x][st.y][st.z] = 1;
        que.push(st);
        while (!que.empty()) {
            Point now = que.front();
            que.pop();
            if (now.x == ed.x && now.y == ed.y && now.z == ed.z) {
                return now.step;
            }
            for (int i = 0 ; i < 6 ; ++i) {
                Point temp(now.x + dx[i], 
                           now.y + dy[i], 
                           now.z + dz[i],
                           now.step + 1);
                if (!check(temp)) continue;
                vis[temp.x][temp.y][temp.z] = 1;
                que.push(temp);
            }
        }
        return -1;
    }
    void solve() {
        int ret = BFS();
        if (ret != -1) {
            printf("Escaped in %d minute(s).
    ", ret);
        } else {
            printf("Trapped!
    ");
        }
    }
    int main() {
        // freopen("./in.in", "r", stdin);
        while (scanf("%d%d%d", &L, &R, &C) != EOF) {
            if (L == 0 && R == 0 && C == 0) break;
            memset(vis, 0, sizeof(vis));
            memset(G, 0, sizeof(G));
            read();
            solve();
        }
        return 0;
    }
    
  • 相关阅读:
    shell管道和重定向
    shell脚本
    Hexo学习笔记--常用命令及部署步骤
    Git学习笔记--Git常用命令
    Spark link集合
    Struts2学习笔记--Struts2的体系结构
    JNI错误记录--JNI程序调用本地库时JVM崩溃
    Hibernate学习笔记--使用ThreadLocal
    Hibernate学习笔记--核心编程
    Hibernate学习笔记--映射配置文件详解
  • 原文地址:https://www.cnblogs.com/WArobot/p/7903466.html
Copyright © 2011-2022 走看看