#include #include using namespace std; int mem[105][105][1<<4]; bool byl[105][105][1<<4]; int kier[][2]={{0,1},{0,-1},{1,0},{-1,0}}; char mapa[105][105]; struct stan{ int x,y,sub; }; stan tem; queue Q; int val[1000]; int main(){ int R,C,xp,yp,x,y,sub,x2,y2,a,b,sub2; val['B']=0; val['Y']=1; val['R']=2; val['G']=3; val['b']=0; val['y']=1; val['r']=2; val['g']=3; while(true){ scanf("%d%d",&R,&C); if(R==0 && C==0) break; for(int i=0;i<105;i++) for(int j=0;j<105;j++) for(int z=0;z<(1<<4);z++){ mem[i][j][z]=1000000; byl[i][j][z]=false; } char c; while(true){ c=getchar(); if(c=='\n') break; } for(int i=0;i=R || y2>=C) continue; if(mapa[x2][y2]=='#') continue; else if(mapa[x2][y2]=='.' || mapa[x2][y2]=='*' || mapa[x2][y2]=='X'){ a=mem[x][y][sub]+1; if(!byl[x2][y2][sub]){ mem[x2][y2][sub]=a; byl[x2][y2][sub]=true; tem.x=x2; tem.y=y2; tem.sub=sub; Q.push(tem); } else if(mem[x2][y2][sub]'A' && mapa[x2][y2]<'Z' && ((sub>>val[mapa[x2][y2]])%2==1) ){ a=mem[x][y][sub]+1; if(!byl[x2][y2][sub]){ mem[x2][y2][sub]=a; byl[x2][y2][sub]=true; tem.x=x2; tem.y=y2; tem.sub=sub; Q.push(tem); } else if(mem[x2][y2][sub]'a' && mapa[x2][y2]<'z'){ if((sub>>val[mapa[x2][y2]])%2) sub2=sub; else sub2=sub+(1<mem[i][j][z]) ) best=mem[i][j][z]; } if(best==-1) printf("The poor student is trapped!\n"); else printf("Escape possible in %d steps.\n",best); } return 0; }