#include struct pole { char z; int n,b,y,r,g; }; struct mark { int x,y,steps; }; int main() { struct mark q[50000]; struct pole plan[100][100]; int rows, cols, i, j, k; char c; while (1) { scanf("%d %d\n", &rows, &cols); if (!rows && !cols) break; int sx,sy; for (i=0; i= cols) continue; if (m[k].y < 0 || m[k].y >= rows) continue; if (plan[m[k].x][m[k].y].z == '#') continue; if (plan[m[k].x][m[k].y].z == 'R' && !plan[p.x][p.y].r) continue; if (plan[m[k].x][m[k].y].z == 'G' && !plan[p.x][p.y].g) continue; if (plan[m[k].x][m[k].y].z == 'Y' && !plan[p.x][p.y].y) continue; if (plan[m[k].x][m[k].y].z == 'B' && !plan[p.x][p.y].b) continue; /* zapis nove nalezenmeho klice */ if (plan[m[k].x][m[k].y].z == 'r') plan[m[k].x][m[k].y].r = 1; if (plan[m[k].x][m[k].y].z == 'g') plan[m[k].x][m[k].y].g = 1; if (plan[m[k].x][m[k].y].z == 'b') plan[m[k].x][m[k].y].b = 1; if (plan[m[k].x][m[k].y].z == 'y') plan[m[k].x][m[k].y].y = 1; int sirit = 0; if (plan[p.x][p.y].n && !plan[m[k].x][m[k].y].n) sirit = 1; if (!sirit && plan[p.x][p.y].y && !plan[m[k].x][m[k].y].y) sirit = 1; if (!sirit && plan[p.x][p.y].b && !plan[m[k].x][m[k].y].b) sirit = 1; if (!sirit && plan[p.x][p.y].g && !plan[m[k].x][m[k].y].g) sirit = 1; if (!sirit && plan[p.x][p.y].r && !plan[m[k].x][m[k].y].r) sirit = 1; if (!sirit) continue; plan[m[k].x][m[k].y].n |= plan[p.x][p.y].n; plan[m[k].x][m[k].y].b |= plan[p.x][p.y].b; plan[m[k].x][m[k].y].r |= plan[p.x][p.y].r; plan[m[k].x][m[k].y].g |= plan[p.x][p.y].g; plan[m[k].x][m[k].y].y |= plan[p.x][p.y].y; /*printf(" adding %d %d\n", m[k].x, m[k].y); */ q[qe++] = (struct mark){ m[k].x, m[k].y, p.steps+1 }; } } if (!found) { printf("The poor student is trapped!\n"); } } return 0; }