#include #define L 1 #define U 2 #define R 4 #define D 8 int omaze[8][8]; int osx,osy; int otx,oty; int maze[8][8]; int visited[8][8]; int sx,sy; int tx,ty; int extra; struct { int x,y; } s[1000]; int sp; void debug () { int i,j; printf ("%d %d %d %d\n", sx, sy, tx, ty); for (i=0;i<7;i++) { for (j=0;j<7;j++) { printf ((maze[i][j] & L) ? "L" : "."); printf ((maze[i][j] & U) ? "U" : "."); printf ((maze[i][j] & D) ? "D" : "."); printf ((maze[i][j] & R) ? "R" : "."); printf (" "); } printf ("\n"); } printf ((extra & L) ? "L" : "."); printf ((extra & U) ? "U" : "."); printf ((extra & D) ? "D" : "."); printf ((extra & R) ? "R" : "."); printf (" "); printf ("\n"); } int solve () { int x,y; // debug (); sp = 0; s[sp].x = sx; s[sp].y = sy; sp++; while (sp--) { x=s[sp].x; y=s[sp].y; if (x == tx && y == ty) return 1; visited[y][x]=1; if (x < 6 && !visited[y][x+1] && (maze[y][x] & R) && (maze[y][x+1] & L)) { s[sp].x=x+1; s[sp].y=y; sp++; } if (x > 0 && !visited[y][x-1] && (maze[y][x] & L) && (maze[y][x-1] & R)) { s[sp].x=x-1; s[sp].y=y; sp++; } if (y > 0 && !visited[y-1][x] && (maze[y][x] & D) && (maze[y-1][x] & U)) { s[sp].x=x; s[sp].y=y-1; sp++; } if (y < 6 && !visited[y+1][x] && (maze[y][x] & U) && (maze[y+1][x] & D)) { s[sp].x=x; s[sp].y=y+1; sp++; } } return 0; } void copy () { int i,j; for (i=0;i<7;i++) for(j=0;j<7;j++) { maze[i][j] = omaze[i][j]; visited[i][j] = 0; } } int insert_l (int r) { int j; copy (); ty = oty; if (ty == r) tx = otx + 1; else tx=otx; if (tx > 6) return 0; sy = osy; if (sy == r) sx = (osx + 1) % 7; else sx=osx; for (j=5;j>=0;j--) maze[r][j+1] = maze[r][j]; maze[r][0] = extra; return solve (); } int insert_r (int r) { int j; copy (); ty = oty; if (ty == r) tx = otx - 1; else tx=otx; if (tx < 0) return 0; sy = osy; if (sy == r) sx = (osx + 6) % 7; else sx = osx; for (j=0;j<6;j++) maze[r][j] = maze[r][j+1]; maze[r][6] = extra; return solve (); } int insert_u (int c) { int j; copy (); tx = otx; if (tx == c) ty = oty - 1; else ty=oty; if (tx < 0) return 0; sx = osx; if (sx == c) sy = (osy + 6) % 7; else sy=osy; for (j=0;j<6;j++) maze[j][c] = maze[j+1][c]; maze[6][c] = extra; return solve (); } int insert_d (int c) { int j; copy (); tx = otx; if (tx == c) ty = oty + 1; else ty=oty; if (tx > 6) return 0; sx = osx; if (sx == c) sy = (osy + 1) % 7; else sy=osy; for (j=5;j>=0;j--) maze[j+1][c] = maze[j][c]; maze[0][c] = extra; return solve (); } void rotate() { extra = extra << 1; if (extra & 16) { extra -=16; extra++; } } int main() { char l[200]; char l1[200]; char l2[200]; char l3[200]; int i,j,r; while (1) { gets (l); sscanf(l,"%d%d%d%d", &osy,&osx,&oty,&otx); if (osy==0 && osx==0 && oty==0 && otx==0) return 0; osy--;osx--;oty--;otx--; gets(l); for (i=0;i<7;i++) { gets(l1); gets(l2); gets(l3); gets(l); for (j=0;j<7;j++) { omaze[i][j] = (l1[j*4+1] == '|') * D + (l3[j*4+1] == '|') * U + (l2[j*4+2] == '-') * R + (l2[j*4+0] == '-') * L; } } gets(l1); gets(l2); gets(l3); gets(l); extra = (l1[1] == '|') * D + (l3[1] == '|') * U + (l2[2] == '-') * R + (l2[0] == '-') * L; if (extra != 15) { for (r=0;r<4;r++) { rotate(); for (i=0;i<3;i++) if (insert_u (i*2+1)) goto ok; for (i=0;i<3;i++) if (insert_l (i*2+1)) goto ok; for (i=0;i<3;i++) if (insert_d (i*2+1)) goto ok; for (i=0;i<3;i++) if (insert_r (i*2+1)) goto ok; } } else { for (i=0;i<3;i++) if (insert_u (i*2+1)) goto ok; for (i=0;i<3;i++) if (insert_l (i*2+1)) goto ok; for (i=0;i<3;i++) if (insert_d (i*2+1)) goto ok; for (i=0;i<3;i++) if (insert_r (i*2+1)) goto ok; } printf ("Bad luck!\n"); continue; ok: printf ("You can win in one move.\n"); } }