#include #include #include using namespace std; int dp[16][100][100]; string mapka[150]; class state{ public: int x, y, keys; state(int xx, int yy, int kk):x(xx),y(yy), keys(kk) {} }; int doors[255]; int keys[255]; int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; void do_it(int r,int c) { doors['B'] = 1; doors['Y'] = 2; doors['R'] = 4; doors['G'] = 8; keys['b'] = 1; keys['y'] = 2; keys['r'] = 4; keys['g'] = 8; for(int i=0;ikolejka; for(int i=0;i= r || ny >= c) continue; char c = mapka[nx][ny]; int tmp_k = k |keys[c]; if(dp[tmp_k][nx][ny] != -1) continue; if(c == '#') continue; if(c == 'X') { //TODO koniec printf("Escape possible in %d steps.\n", dp[k][x][y] + 1); return; } if(c == 'B' || c == 'R' || c == 'Y' || c == 'G') { if(!(tmp_k & doors[c])) { continue; } } dp[tmp_k][nx][ny] = dp[k][x][y] + 1; kolejka.push(state(nx, ny, tmp_k)); } } printf("The poor student is trapped!\n"); } int main() { int n,m; scanf("%i %i",&n,&m); while(n != 0 && m != 0) { do_it(n,m); scanf("%i %i",&n,&m); } }