【POJ2251】Dungeon Master

problem

solution

codes

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n, m, o, a[40][40][40], vis[40][40][40];
int sx, sy, sz, ex, ey, ez, ans;
const int dx[] = {1,-1,0,0,0,0};
const int dy[] = {0,0,1,-1,0,0};
const int dz[] = {0,0,0,0,1,-1};
struct node{
    int x, y, z, step;
    node(int x = 0, int y = 0, int z = 0, int step = 0):x(x),y(y),z(z),step(step){}
};
void bfs(){
    queue<node>q;
    q.push(node(sx,sy,sz,0));
    a[sx][sy][sz] = 1;
    //vis[sx][sy][sz] = 1;
    while(!q.empty()){
        node t = q.front();  q.pop();
        if(t.x==ex && t.y==ey && t.z==ez){
            ans = t.step;
        }
        for(int i = 0; i < 6; i++){
            int nx = t.x+dx[i], ny = t.y+dy[i], nz = t.z+dz[i];
            if(nx>0 && nx<=n && ny>0 && ny<=m && nz>0 && nz<=o && !a[nx][ny][nz]){
                q.push(node(nx,ny,nz,t.step+1));
                a[nx][ny][nz]  = 1;
                //vis[nx][ny][nz] = 1;
            }
        }
    }
}
int main(){
    while(cin>>n>>m>>o &&n){
        ans = 0;
        memset(a,0,sizeof(a));
        //memset(vis,0,sizeof(vis));
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                for(int k = 1; k <= o; k++){
                    char ch;  cin>>ch;
                    if(ch == '#')a[i][j][k] = 1;
                    if(ch == 'S'){ sx = i; sy = j; sz = k;}
                    if(ch == 'E'){ ex = i; ey = j; ez = k;}
                }
            }
        }
        bfs();
        if(ans)cout<<"Escaped in "<<ans<<" minute(s).
";
        else cout<<"Trapped!
";
    }
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444804.html