pku poj 2251

pku poj 2251
这题想起魔方了,改改A了2225
#include < iostream >
using   namespace  std;
char  dung[ 35 ][ 35 ][ 35 ];
bool  visit[ 35 ][ 35 ][ 35 ];
int  dx[ 6 ] = {-1,0,0,0,0,1} ;
int  dy[ 6 ] = {0,-1,0,0,1,0} ;
int  dz[ 6 ] = {0,0,-1,1,0,0} ;
int  que[ 82000 ]; // 3*30*30*30,一开始开得太小了2700,27000,30000,40000,50000,640000全TM的WA||RE
int  L,R,C;
int  sx,sy,sz,ex,ey,ez;
int  step;
void  bfs()
{
    
int i,x,y,z,tx,ty,tz;
    
int rear,front,trear;
    
bool reach=false;
    memset(que,
0,sizeof(que));
    rear
=front=0;
    que[rear
++]=sx;
    que[rear
++]=sy;
    que[rear
++]=sz;
    visit[sx][sy][sz]
=true;
    
while(front<rear )//&& !visit[ex][ey][ez])
    {
        
if(visit[ex][ey][ez]) {reach=true;break;}//找到E,停
        trear=rear;
        step
++;
        
while(front<trear )//&& !visit[ex][ey][ez])
        {
            
//if(visit[ex][ey][ez]) {reach=true;break;}
            x=que[front++];
            y
=que[front++];
            z
=que[front++];
            
for(i=0;i<6;i++)
            
{
                tx
=x+dx[i];
                ty
=y+dy[i];
                tz
=z+dz[i];
                
if(tx>=0 && tx<&& ty>=0 && ty<&& tz>=0 && tz<&&  !visit[tx][ty][tz] && dung[tx][ty][tz]!='#')
                
{
                    visit[tx][ty][tz]
=true;
                    que[rear
++]=tx;
                    que[rear
++]=ty;
                    que[rear
++]=tz;
                }

            }
//for
        }
//while
    }
//while
    if(!reach) step=-1;//找不到E,trapped
}

int  main()
{
    
int i,j,k;
    
while(scanf("%d%d%d",&L,&R,&C)!=EOF && L && R && C)
    
{
        memset(visit,
0,sizeof(visit));
        
for(i=0;i<L;i++)
            
for(j=0;j<R;j++)
                scanf(
" %s",dung[i][j]);
        
for(i=0;i<L;i++)
            
for(j=0;j<R;j++)
                
for(k=0;k<C;k++)
                
{
                    
if(dung[i][j][k]=='S'{sx=i;sy=j;sz=k;}
                    
else if(dung[i][j][k]=='E'{ex=i;ey=j;ez=k;}
                }

        step
=0;
        bfs();
        
if(step==-1) printf("Trapped!\n");
        
else printf("Escaped in %d minute(s).\n",step);

    }

    
return 0;
}

你可能感兴趣的:(pku poj 2251)