Problem
You find yourself standing outside of a perfect maze. A maze is defined as "perfect" if it meets the following conditions:
- It is a rectangular grid of rooms, R rows by C columns.
- There are exactly two openings on the outside of the maze: the entrance and the exit. The entrance is always on the north wall, while the exit could be on any wall.
- There is exactly one path between any two rooms in the maze (that is, exactly one path that does not involve backtracking).
You decide to solve the perfect maze using the "always turn left" algorithm, which states that you take the leftmost fork 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.
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? |
---|---|---|---|---|
1 | Yes | No | No | No |
2 | No | Yes | No | No |
3 | Yes | Yes | No | No |
4 | No | No | Yes | No |
5 | Yes | No | Yes | No |
6 | No | Yes | Yes | No |
7 | Yes | Yes | Yes | No |
8 | No | No | No | Yes |
9 | Yes | No | No | Yes |
a | No | Yes | No | Yes |
b | Yes | Yes | No | Yes |
c | No | No | Yes | Yes |
d | Yes | No | Yes | Yes |
e | No | Yes | Yes | Yes |
f | Yes | Yes | Yes | Yes |
Limits
1 ≤ N ≤ 100.
Small dataset
2 ≤ len(entrance_to_exit) ≤ 100, 2 ≤ len(exit_to_entrance) ≤ 100.
Large dataset
2 ≤ len(entrance_to_exit) ≤ 10000, 2 ≤ len(exit_to_entrance) ≤ 10000.
Sample
Input |
2 WRWWLWWLWWLWLWRRWRWWWRWWRWLW WWRRWLWLWWLWWLWWRWWRWWLW WW WW |
Output |
Case #1: ac5 386 9c7 e43 9c5 Case #2: 3 |
// leekun 2008.06.30
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define nouth 0
#define west 1
#define south 2
#define east 3
#define turnleft(dir) ((dir+1)%4)
#define turnright(dir) ((dir+3)%4)
ofstream outfile("B_large.out");
class grid
{
public:
grid(int x = 0, int y = 0) : X(x), Y (y), next(0) {
bwall[0] = 0; bwall[1] = 0; bwall[2] = 0; bwall[3] = 0;}
bool bwall[4];
grid * next;
// int Direct;
int X, Y;
};
string inttostring_bytarget(int num, string target)
{
string ret = "";
int B = target.length();
if (num==0) return "0";
while (num>0)
{
ret = target[num%B] + ret;
num /= B;
}
return ret;
}
void drawmap(grid * map, int row, int columns)
{
int i = 0;
int maplen = row * columns;
while (i < maplen)
{
if ((i%columns==0)&&(i>0))
{
cout<<endl;
outfile<<endl;
}
bool up = (i >= columns )? map[i-columns].bwall[south]: 0;
bool down = (i+columns < row*columns)? map[i+columns].bwall[nouth]:0;
bool left = (i%columns > 0)? map[i-1].bwall[east]: 0;
bool right = (i%columns < columns-1)? map[i+1].bwall[west]: 0;
int V =
(map[i].bwall[nouth] || up) +
(map[i].bwall[south] || down) * 2 +
(map[i].bwall[west] || left) * 4 +
(map[i].bwall[east] || right) * 8;
V = 15 - V;
string S = inttostring_bytarget(V, "0123456789abcdef");
cout << S;
outfile << S;
i++;
}
cout << endl;
outfile << endl;
}
int main(int argc, char *argv[])
{
ifstream infile("B-large.in");
int N = 0;
infile >> N;
int I = 1;
while (N-- > 0)
{
string entrance_to_exit, exit_to_entrance;
infile >> entrance_to_exit >> exit_to_entrance;
// process the entrance_to_exit
int p = 1;
int q = 1;
int Lenp = entrance_to_exit.length();
int Lenq = exit_to_entrance.length();
grid entrance(0, 0);
grid *node = &entrance;
int max_eage[4] = {0};
int curdir = south;
int pX = 0;
int pY = 0;
bool setexit_lock = false;
int exitX = 0, exitY = 0 , exitDir= curdir;
while (1)
{
char C;
if (p < Lenp-1) // loop 1
{
C = entrance_to_exit[p];
p++;
}
else if (q < Lenq-1) // loop 2
{
// 只記錄一次出口位置
if (!setexit_lock)
{
exitX = pX;
exitY = pY;
exitDir = curdir;
setexit_lock = true;
curdir = turnright(turnright(curdir));
}
C = exit_to_entrance[q];
q++;
}
else
break;
// 向前走只改變當前座標
if ('W' == C)
{
switch(curdir){
case nouth: pY--;
break;
case south: pY++;
if(pY > max_eage[south])
max_eage[south] = pY;
break;
case west: pX--;
if(pX < max_eage[west])
max_eage[west] = pX;
break;
case east: pX++;
if(pX > max_eage[east])
max_eage[east] = pX;
break;
}
grid *newnode = new grid(pX, pY);
node->next = newnode;
// node->Direct = curdir;
node = newnode;
}
// 向左轉只是說明左邊有路走,僅此而已
else if('L' == C)
{
curdir = turnleft(curdir);
}
// 向右轉說明當前前方和左方都是牆
else if ('R' == C)
{
node->bwall[curdir] = 1;
node->bwall[turnleft(curdir)] = 1;
curdir = turnright(curdir);
}
}
// creat a map
int rows = max_eage[south] - max_eage[nouth] + 1;
int columns = max_eage[east] - max_eage[west] + 1;
// 座標轉換差值
int dx = 0 - max_eage[west];
grid * map = new grid[rows * columns];
grid * nextnode = &entrance;
while (nextnode)
{
int position = nextnode->X + dx + nextnode->Y * columns;
for (int i = 0; i<4; i++)
{
map[position].bwall[i] = map[position].bwall[i] || nextnode->bwall[i];
}
grid * t = nextnode;
nextnode = nextnode->next;
if (t != &entrance) delete t;
}
// 寫map邊界
int k;
for (k = 0; k < rows; k++)
{
map[k * columns].bwall[west] = 1;
map[(k+1) * columns - 1].bwall[east] =1;
}
for (k = 0; k < columns; k++)
{
map[k].bwall[nouth] = 1;
map[rows * columns - 1 - k].bwall[south] =1;
}
// 入口出口
map[entrance.X + dx].bwall[nouth] = 0;
map[exitX + dx + exitY*columns].bwall[exitDir] = 0;
// drawmap
cout << "Case #" << I << ": " << endl;
outfile << "Case #" << I << ": " << endl;
I++;
drawmap(map, rows, columns);
delete []map;
}
return 0;
}