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(){
    };
    bool operator <(const node &a)const {
        return steps>a.steps;
    }
};
int bfs();
priority_queue<node> q;
int l,r,c;
node s,e;
][][];
] = {{,,},{-,,},{,,},{,-,},{,,},{,,-}};
int main()
{
//    freopen("in.txt","r",stdin);
    while(cin>>l>>r>>c,l+r+c){
        ; i<l; ++i){
            ; j<r; ++j){
                cin>>map[i][j];
                ; 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;
    }
    ;
}
int bfs(){
    while(!q.empty()) q.pop();
    q.push(s);
    while(!q.empty()){
        node temp = q.top();q.pop();
        ; i<; ++i){
            node a = temp;
            a.z += dir[i][];
            a.x += dir[i][];
            a.y += dir[i][];
            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;
             || a.z>=l)continue;
             || a.x>=r)continue;
             || a.y>=c)continue;
            q.push(a);
            map[a.z][a.x][a.y] = '#';
        }
    }
    ;
}
上一篇:ECMAScript 6教程 (一)


下一篇:js、jQuery实现2048小游戏