POJ-2251 3维BFS..

      听起来嚎高端的感觉...其实就是大水题..所谓3维..就是在地图上除了能前后左右4个方向拓展..还能上下放下拓展...就是写的时候有点小长..


Program:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;   
struct node
{
      int x,y,z,step;
}h,p;
int L,R,C,i,j,k,Gx,Gy,Gz;
char s[35][35][35];
bool f;
queue<node> myqueue; 
int main()
{   
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);   
      while (~scanf("%d%d%d",&L,&R,&C))
      {
            if (!L) break; 
            while (!myqueue.empty()) myqueue.pop();
            f=false;
            for (i=1;i<=L;i++)
            {
                  for (j=1;j<=R;j++) 
                  {
                         scanf("%s",s[i][j]+1); 
                         for (k=1;k<=C;k++) 
                         if (s[i][j][k]=='S')
                         {
                                h.y=j;  h.x=k;
                                h.z=i;  h.step=0;
                                s[i][k][k]='#';
                                myqueue.push(h);
                         }else
                         if (s[i][j][k]=='E')
                         {
                                Gy=j;
                                Gx=k;
                                Gz=i;
                         }                  
                  }
            }
            while (!myqueue.empty())
            {
                  h=myqueue.front();
                  myqueue.pop();
                  if (h.z!=1 && s[h.z-1][h.y][h.x]!='#')
                  {
                          if (s[h.z-1][h.y][h.x]=='E') 
                          {
                                  printf("Escaped in %d minute(s).\n",h.step+1);
                                  goto A;
                          }
                          p.z=h.z-1;   p.y=h.y;
                          p.x=h.x;     p.step=h.step+1;
                          s[h.z-1][h.y][h.x]='#';
                          myqueue.push(p);                        
                  }
                  if (h.z!=L && s[h.z+1][h.y][h.x]!='#')
                  {
                          if (s[h.z+1][h.y][h.x]=='E') 
                          {
                                  printf("Escaped in %d minute(s).\n",h.step+1);
                                  goto A;
                          }
                          p.z=h.z+1;   p.y=h.y;
                          p.x=h.x;     p.step=h.step+1;
                          s[h.z+1][h.y][h.x]='#';
                          myqueue.push(p);                    
                  }
                  if (h.x!=1 && s[h.z][h.y][h.x-1]!='#')
                  {
                          if (s[h.z][h.y][h.x-1]=='E') 
                          {
                                  printf("Escaped in %d minute(s).\n",h.step+1);
                                  goto A;
                          }
                          p.z=h.z;   p.y=h.y;
                          p.x=h.x-1;     p.step=h.step+1;
                          s[h.z][h.y][h.x-1]='#';
                          myqueue.push(p);                    
                  }          
                  if (h.x!=C && s[h.z][h.y][h.x+1]!='#')
                  {
                          if (s[h.z][h.y][h.x+1]=='E') 
                          {
                                  printf("Escaped in %d minute(s).\n",h.step+1);
                                  goto A;
                          }
                          p.z=h.z;   p.y=h.y;
                          p.x=h.x+1;     p.step=h.step+1;
                          s[h.z][h.y][h.x+1]='#';
                          myqueue.push(p);                    
                  }      
                  if (h.y!=1 && s[h.z][h.y-1][h.x]!='#')
                  {
                          if (s[h.z][h.y-1][h.x]=='E') 
                          {
                                  printf("Escaped in %d minute(s).\n",h.step+1);
                                  goto A;
                          }
                          p.z=h.z;   p.y=h.y-1;
                          p.x=h.x;     p.step=h.step+1;
                          s[h.z][h.y-1][h.x]='#';
                          myqueue.push(p);                    
                  }          
                  if (h.y!=R && s[h.z][h.y+1][h.x]!='#')
                  {
                          if (s[h.z][h.y+1][h.x]=='E') 
                          {
                                  printf("Escaped in %d minute(s).\n",h.step+1);
                                  goto A;
                          }
                          p.z=h.z;   p.y=h.y+1;
                          p.x=h.x;     p.step=h.step+1;
                          s[h.z][h.y][h.x+1]='#';
                          myqueue.push(p);                    
                  }                                         
            } 
            printf("Trapped!\n");
            A:;
      }
      return 0;
}


你可能感兴趣的:(POJ-2251 3维BFS..)