zoukankan      html  css  js  c++  java
  • NYOJ--353--bfs+优先队列--3D dungeon

    /*
        Name: NYOJ--3533D dungeon
        Author: shen_渊 
        Date: 15/04/17 15:10
        Description: bfs()+优先队列,队列也能做,需要开一个vis[35][35][35]标记
    */
    
    #include<iostream>
    #include<queue>
    using namespace std;
    struct node{
        int x,y,z,steps;
        node():steps(0){
        };
        bool operator <(const node &a)const {
            return steps>a.steps;
        }
    };
    int bfs();
    priority_queue<node> q;
    int l,r,c;
    node s,e;
    char map[35][35][35];
    int dir[][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
    int main()
    {
    //    freopen("in.txt","r",stdin);
        while(cin>>l>>r>>c,l+r+c){
            for(int i=0; i<l; ++i){
                for(int j=0; j<r; ++j){
                    cin>>map[i][j];
                    for(int k=0; k<c; ++k){
                        if(map[i][j][k] == 'S')s.x = j,s.z = i,s.y = k;
                        if(map[i][j][k] == 'E')e.x = j,e.y = k,e.z = i;
                    }
                }
            }
            int t;
            if (t=bfs())
                cout << "Escaped in " << t << " minute(s)." << endl;
            else
                cout << "Trapped!" << endl;
        }
        return 0;
    }
    int bfs(){
        while(!q.empty()) q.pop();
        q.push(s);
        while(!q.empty()){
            node temp = q.top();q.pop();
            for(int i=0; i<6; ++i){
                node a = temp;
                a.z += dir[i][0];
                a.x += dir[i][1];
                a.y += dir[i][2];
                a.steps++;
                if(a.z == e.z&& a.x==e.x&&a.y==e.y)return a.steps;
                if(map[a.z][a.x][a.y] == '#')continue;
                if(a.z<0 || a.z>=l)continue;
                if(a.x<0 || a.x>=r)continue;
                if(a.y<0 || a.y>=c)continue;
                q.push(a);
                map[a.z][a.x][a.y] = '#';
            }
        }
        return 0;    
    }
  • 相关阅读:
    c++ 11 thread 初试
    java UDP聊天与文件传输
    iOS 基础类解析
    Hadoop HA高可用集群搭建(2.7.2)
    object-c 不定參数的遍历和原理
    9.4 返回更新后的行
    java面向接口编程
    Node.js开发入门—套接字(socket)编程
    shell脚本输出带颜色字体
    shell--read命令
  • 原文地址:https://www.cnblogs.com/slothrbk/p/7251886.html
Copyright © 2011-2022 走看看