POJ-2251

来源:互联网 发布:巨灵金融怎么查数据 编辑:程序博客网 时间:2024/05/14 16:40
#include <iostream>#include <string.h>#include <stdio.h>#include <queue>using namespace std;struct m{    int x;    int y;    int l;    int bu;};int L,row,col;char a[35][35][35];int visited[35][35][35];queue<m>Q;int dx[6]={0,0,-1,1,0,0};int dy[6]={-1,1,0,0,0,0};int dz[6]={0,0,0,0,1,-1};int main(){    while(~scanf("%d%d%d",&L,&row,&col)){        if(L==0&row==0&&col==0)break;    memset(visited,0,sizeof(visited));    int i=0,j=0,k=0;    for(i=0;i<L;i++)    for(j=0;j<row;j++)    scanf("%s",&a[i][j]);    int ans=-1;     m s,e;     s.bu=-1;    for(i=0;i<L;i++)    for(j=0;j<row;j++)    for(k=0;k<col;k++)    if(a[i][j][k]=='S'){        s.l=i;s.x=k;s.y=j;    }    else if(a[i][j][k]=='E'){        e.l=i;e.x=k;e.y=j;    }    //cout<<e.l<<e.x<<e.y<<endl;    while(!Q.empty())Q.pop();    Q.push(s);    while(!Q.empty()){        m t=Q.front();        Q.pop();        m temp;        temp.bu=t.bu+1;        temp.l=t.l;        temp.x=t.x;        temp.y=t.y;        if(temp.l==e.l&&temp.x==e.x&&temp.y==e.y){        ans=temp.bu;        }        for(int T=0;T<6;T++){            temp.x=t.x+dx[T];            temp.y=t.y+dy[T];            temp.l=t.l+dz[T];            if(temp.x>=0&&temp.x<col&&temp.y>=0&&temp.y<row&&(a[temp.l][temp.y][temp.x]=='.'||a[temp.l][temp.y][temp.x]=='E')&&visited[temp.l][temp.y][temp.x]==0){                visited[temp.l][temp.y][temp.x]=1;                Q.push(temp);            }        }    }    if(ans==-1)cout<<"Trapped!"<<endl;    else cout<<"Escaped in "<<ans<<" minute(s)."<<endl;    }    return 0;}

0 0
原创粉丝点击