Friday, January 16, 2009

Solve the Maze

There is a maze with following characteristics:

  1. It is a rectangular grid of blocks, R rows and C columns.
  2. There are only two openings on the outside of the maze: one is the entrance and the other is exit. The entrance is always on the north wall, while the exit could be on any wall.
  3. There is only one path between any two blocks in the maze (i.e., only one path that does not involve backtracking).

You have to solve the maze by the "turning left algorithm", which says that you take the leftmost turn at every opportunity. If you hit a dead end, you turn right twice (180 degrees clockwise) and continue. (If you were to stick out your left arm and touch the wall while following this algorithm, you'd solve the maze without ever breaking contact with the wall.) Once you finish the maze, you decide to go the extra step and solve it again (still always turning left), but starting at the exit and finishing at the entrance.

The path you take through the maze can be described with three characters: 'W' means to walk forward into the next room, 'L' means to turn left (or counterclockwise) 90 degrees, and 'R' means to turn right (or clockwise) 90 degrees. You begin outside the maze, immediately adjacent to the entrance, facing the maze. You finish when you have stepped outside the maze through the exit. For example, if the entrance is on the north and the exit is on the west, your path through the following maze would be WRWWLWWLWWLWLWRRWRWWWRWWRWLW:

If the entrance and exit were reversed such that you began outside the west wall and finished out the north wall, your path would be WWRRWLWLWWLWWLWWRWWRWWLW. Given your two paths through the maze (entrance to exit and exit to entrance), your code should return a description of the maze.

Now write a code which takes a text file as its input and write an output file described as follows:

Input

The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as

entrance_to_exit exit_to_entrance

All paths will be at least two characters long, consist only of the characters 'W', 'L', and 'R', and begin and end with 'W'.

Output

For each test case, output one line containing "Case #x:" by itself. The next R lines give a description of the R by C maze. There should be C characters in each line, representing which directions it is possible to walk from that room. Refer to the following legend:

Character Can walk north? Can walk south? Can walk west? Can walk east?
1YesNoNoNo
2NoYesNoNo
3YesYesNoNo
4NoNoYesNo
5YesNoYesNo
6NoYesYesNo
7YesYesYesNo
8NoNoNoYes
9YesNoNoYes
aNoYesNoYes
bYesYesNoYes
cNoNoYesYes
dYesNoYesYes
eNoYesYesYes
fYesYesYesYes

Limits

1 ≤ N ≤ 100.

2 ≤ len(entrance_to_exit) ≤ 100,
2 ≤ len(exit_to_entrance) ≤ 100.

Sample Input/Output Files:


Input
2
WRWWLWWLWWLWLWRRWRWWWRWWRWLW WWRRWLWLWWLWWLWWRWWRWWLW
WW WW

Output
Case #1:
ac5
386
9c7
e43
9c5
Case #2:
3