Advertisement
Emiliatan

c124

Mar 29th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. /* c124            */
  2. /* AC (4ms, 388KB) */
  3. #include <cstdio>
  4. #include <queue>
  5. #include <cstring>
  6. #define in(x,y,z) ((x)>=0&&(x)<L&&(y)>=0&&(y)<R&&(z)>=0&&(z)<C)
  7.  
  8. using namespace std;
  9.  
  10. const short Block=-2,Undefine=-1;
  11. const short dir[2]={-1,1};
  12. int L,R,C;
  13. int space[30][30][30],sx,sy,sz,ex,ey,ez;
  14. struct point {int x,y,z;};
  15.  
  16. void bfs()
  17. {
  18.     queue<point> qu;
  19.  
  20.     if(space[sx][sy][sz]==Block) {puts("Trapped!"); return;}
  21.  
  22.     space[sx][sy][sz]=0;
  23.     qu.emplace(point{sx,sy,sz});
  24.  
  25.     while(qu.size())
  26.     {
  27.         int x=qu.front().x,y=qu.front().y,z=qu.front().z; qu.pop();
  28.         int nexx,nexy,nexz;
  29.  
  30.         if(x==ex&&y==ey&&z==ez) break;
  31.  
  32.         for(int index=0;index<3;index++)
  33.             for(int i=0;i<2;i++)
  34.             {
  35.                 nexx=x,nexy=y,nexz=z;
  36.  
  37.                 if(index==0) nexx+=dir[i];
  38.                 else if(index==1) nexy+=dir[i];
  39.                 else if(index==2) nexz+=dir[i];
  40.  
  41.                 if(in(nexx,nexy,nexz)&&space[nexx][nexy][nexz]!=Block&&space[nexx][nexy][nexz]==Undefine)
  42.                 {
  43.                     space[nexx][nexy][nexz]=space[x][y][z]+1;
  44.                     qu.emplace(point{nexx,nexy,nexz});
  45.                 }
  46.             }
  47.  
  48.     }
  49.     if(space[ex][ey][ez]==Undefine) puts("Trapped!");
  50.     else printf("Escaped in %d minute(s).\n",space[ex][ey][ez]);
  51. }
  52.  
  53. int main()
  54. {
  55.     while(~scanf("%d %d %d",&L,&R,&C)&&getchar()&&(L||R||C))
  56.     {
  57.         memset(space,Undefine,sizeof(int)*30*30*30);
  58.  
  59.         for(int x=0,ch;x<L;getchar(),x++)
  60.             for(int y=0;y<R;getchar(),y++)
  61.                 for(int z=0;z<C&&(ch=getchar());z++)
  62.                     if(ch=='#') space[x][y][z]=Block;
  63.                     else if(ch=='S') sx=x,sy=y,sz=z;
  64.                     else if(ch=='E') ex=x,ey=y,ez=z;
  65.  
  66.         bfs();
  67.     }
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement