POJ 2251 Dungeon Master (bfs)

 

 

#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

char mat[50][50][50];

int vis[50][50][50];

int op[6][3]={0,-1,0, 0,1,0, 1,0,0, -1,0,0 ,0,0,1, 0,0,-1 };

int ok;

int z,n,m;

struct Node{

  int x,y,z;

  int rt;

};

Node now[200000];

bool isok(Node node)

{

    if(node.z<1||node.z>z||node.x<1||node.x>n||node.y<0||node.y>=m)

        return false;

    if(vis[node.z][node.x][node.y]==1||mat[node.z][node.x][node.y]=='#')

        return false;

    return true;

}

void bfs(int si,int sj,int sk)  //z x y

{

   int front,rear;

   int x,y,z;

   front=0,rear=1;

   z=now[0].z=si;

   x=now[0].x=sj;

   y=now[0].y=sk;

   now[0].rt=0;

   vis[z][x][y]=1;

   while(front<rear)

   {

      // Node temp;

      z=now[front].z;

      x=now[front].x;

      y=now[front].y;

       if(mat[z][x][y]=='E')

       {

           ok=1;

           printf("Escaped in %d minute(s).\n",now[front].rt);

       }



       for(int i=0;i<6;i++)

       {

           z=now[rear].z=now[front].z+op[i][0];

           x=now[rear].x=now[front].x+op[i][1];

           y=now[rear].y=now[front].y+op[i][2];

             now[rear].rt=now[front].rt+1;



           if(isok(now[rear]))

           {

               vis[z][x][y]=1;

               rear++;

           }

       }

       front++;

   }



}

int main()

{



    int i,j,k;

    int si,sj,sk;

    while(scanf("%d%d%d",&z,&n,&m)!=EOF)

    {

        if(z==0&&n==0&&m==0) break;

        ok=0;

        memset(vis,0,sizeof(vis));

        for(i=1;i<=z;i++)

        {

            for(j=1;j<=n;j++)

            {

               scanf("%s",mat[i][j]);

               for(k=0;k<m;k++)

               {

                   if(mat[i][j][k]=='S')

                    {

                       si=i;sj=j;sk=k;

                    }

               }

            }

        }

        bfs(si,sj,sk);

        if(ok==0) printf("Trapped!\n");



    }

    return 0;

}

 

你可能感兴趣的:(master)