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; }