• POJ 2251 Dungeon Master


    题目链接http://poj.org/problem?id=2251


    解析

    简单的三维BFS问题。


    代码

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <set>
    #include <map>
    using namespace std;
    #define Lowbit(x) ((x)&(-(x)))
    #define ll long long
    #define mp make_pair
    #define ff first
    #define ss second
    #define pb push_back
    #define mod 10000007
    //#define LOCAL
    #define MAXN 100010
    #define INF 1e9
    #define Max 100010
    
    int tx[]={0,0,0,0,1,-1};
    int ty[]={1,0,-1,0,0,0};
    int tz[]={0,1,0,-1,0,0};
    
    char str[35][35][35];
    bool visit[35][35][35];
    
    typedef struct node{
        int x,y,z;
        int cost;
    }NODE;
    int L,R,C;
    
    bool check(int x,int y,int z){
        if(x<0||x>=L||y<0||y>=R||z<0||z>=C||visit[x][y][z]||str[x][y][z]!='.')
            return false;
        visit[x][y][z] = true;
        return true;
    }
    
    int main(){
        while(~scanf("%d%d%d", &L,&R,&C),L||R||C){
            int i,j,k;
            for(i=0; i<L; ++i){
                for(j=0; j<R; ++j){
                    scanf("%s", str[i][j]);
                }
            }
    
            NODE now,tar;
            for(i=0; i<L; ++i){
                for(j=0; j<R; ++j){
                    for(k=0; k<C; ++k){
                        if(str[i][j][k]=='S'){
                            now.x = i;
                            now.y = j;
                            now.z = k;
                            str[i][j][k]='.';
                        }
                        else if(str[i][j][k]=='E'){
                            tar.x = i;
                            tar.y = j;
                            tar.z = k;
                            str[i][j][k]='.';
                        }
                    }
                }
            }
    
            //BFS
            memset(visit,false,sizeof(visit));
            queue<NODE> que;
            now.cost = 0;
            que.push(now);
            visit[now.x][now.y][now.z] = true;
            int ans = -1;
    
            while(que.size()){
                now = que.front();
                que.pop();
                if(now.x==tar.x&&now.y==tar.y&&now.z==tar.z){
                    ans = now.cost;
                    break;
                }
                for(i=0; i<6; ++i){
                    NODE tmp;
                    tmp.x = now.x+tx[i];
                    tmp.y = now.y+ty[i];
                    tmp.z = now.z+tz[i];
                    tmp.cost = now.cost+1;
                    if(check(tmp.x,tmp.y,tmp.z)){
                        que.push(tmp);
                    }
                }
            }
    
            if(-1==ans)
                printf("Trapped!
    ");
            else
                printf("Escaped in %d minute(s).
    ", ans);
        }
        return 0;
    }

  • 相关阅读:
    第六章.md
    记录一道字节面试题引出的this指向问题
    记录Vue常问问题整合
    记录Event Loop事件循环、微任务、宏任务
    简单的小问题
    添加汇总行1(Power Query)
    被筛选排除在外的最后一行
    查找指定颜色
    Power Query示例汇总
    动态变化的求和区域
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5138889.html
Copyright © 2020-2023  润新知