Problem
You find yourself standing outside of a perfect maze. A maze is defined as "perfect" if it meets the following conditions:
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:
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 |
#include <iostream> #include <fstream> #include <map> #include <iomanip> //#define MY_DEBUG using namespace std; /* +----------->i | | | V j */ struct maze_coord { maze_coord() { i = j = 0; } maze_coord(int i_, int j_) { i = i_; j = j_; } int i; int j; //for being put into map bool operator < (const maze_coord& other) const { return (j < other.j) || (j == other.j && i < other.i); } //for calculating the size of the maze maze_coord operator - (const maze_coord& other) const { return maze_coord(i - other.i, j - other.j); } }; enum direction { north = 1, south = 1<<1, west = 1<<2, east = 1<<3 }; #ifdef MY_DEBUG //direction symbol const char* symbol = " ^V < >"; #endif //clockwise cycle const direction cycle[4] = {north, east, south, west}; //helper function to turn the direction //turn for |n|*90 degrees //n<0 to turn left //n>0 to turn right direction turn(direction d, int n = 1) { for(int i = 0; i < 4; i++) { if(cycle[i] == d) { i = (i+n)%4; return cycle[(i>=0)?i:(4+i)]; } } } // high 4 bits are used to mark known/unknown status // low 4 bits are used to mark can/cant walk status // order: // east west south north typedef unsigned char room_state; typedef map<maze_coord, room_state> Maze; //Rat will walk in the maze, and mark the state of every room class Rat { public: Rat(Maze& maze) :m_maze(maze) { m_heading = south; m_coord.i = 0; m_coord.j = -1; } //W void Walk(bool is_next_step_walk) { //If there is record for the last room //Mark it as walkable if(m_maze.count(m_coord)) { m_maze[m_coord] |= m_heading; m_maze[m_coord] |= m_heading<<4; } //Walk switch(m_heading) { case north : --m_coord.j;break; case south : ++m_coord.j;break; case west : --m_coord.i;break; case east : ++m_coord.i;break; } //If there is no record for the current room //Clear its state if(!m_maze.count(m_coord)) m_maze[m_coord] = 0x00; direction walkable_wall = turn(m_heading, 2); m_maze[m_coord] |= walkable_wall; //it's walkable m_maze[m_coord] |= walkable_wall<<4; //it's known //WW if(is_next_step_walk) { //left direction unwalkable_wall = turn(m_heading, -1); m_maze[m_coord] |= unwalkable_wall<<4; //it's known to be unwalkable } #ifdef MY_DEBUG cout << "Walk to "; cout << dec << '(' << m_coord.i << ',' << m_coord.j << ')' << ''; cout << "Heading "<< symbol[m_heading] << ":"; cout << hex << (unsigned int)m_maze[m_coord] << endl; #endif } //L void TurnLeft() { //turn left m_heading = turn(m_heading, -1); //left m_maze[m_coord] |= m_heading; //it's walkable m_maze[m_coord] |= m_heading<<4; //it's known #ifdef MY_DEBUG cout << "Turn left " << ''; cout << "Heading "<< symbol[m_heading] << ":"; cout << hex << (unsigned int)m_maze[m_coord] << endl; #endif } //R void TurnRight() { //left direction unwalkable_wall = turn(m_heading, -1); m_maze[m_coord] |= unwalkable_wall<<4; //it's known to be unwalkable //forward unwalkable_wall = m_heading; m_maze[m_coord] |= unwalkable_wall<<4; //it's known to be unwalkable //right m_heading = turn(m_heading, 1); //turn right m_maze[m_coord] |= m_heading; //it's walkable m_maze[m_coord] |= m_heading<<4; //it's known #ifdef MY_DEBUG cout << "Turn right " << ''; cout << "Heading "<< symbol[m_heading] << ":"; cout << hex << (unsigned int)m_maze[m_coord] << endl; #endif } //RR void TurnBack() { //left direction unwalkable_wall = turn(m_heading, -1); m_maze[m_coord] |= unwalkable_wall<<4; //it's known to be unwalkable //forward unwalkable_wall = m_heading; m_maze[m_coord] |= unwalkable_wall<<4; //it's known to be unwalkable //right unwalkable_wall = turn(m_heading, 1); m_maze[m_coord] |= unwalkable_wall<<4; //it's known to be unwalkable //turn back m_heading = turn(m_heading, 2); #ifdef MY_DEBUG cout << "Turn back " << ''; cout << "Heading "<< symbol[m_heading] << ":"; cout << hex << (unsigned int)m_maze[m_coord] << endl; #endif } //Clear the record for current room //call it after gets out of the exit and TurnBack void ClearCurrentRoom() { m_maze.erase(m_coord); #ifdef MY_DEBUG cout << "+++++++++++++" << endl; #endif } //for speeding up the programming, break the encapsulation a little direction m_heading; maze_coord m_coord; Maze& m_maze; }; int main(int argc, char* argv[]) { #ifndef MY_DEBUG //Only for memerizing usage for myself if(argc != 3) { cout << "Usage: atl INPUT_FILE OUTPUT_FILE" << endl; return 0; } ifstream fin(argv[1]); //!! ofstream fout(argv[2]); //!! #else ifstream fin("B-large.in"); //!! ofstream fout("test.out"); //!! #endif //How many cases in total? int case_max; fin >> case_max; fin.ignore(); // '' char tmp; for(int cur_case = 1; cur_case <= case_max; cur_case++) { Maze maze; Rat rat(maze); while((tmp = fin.get()) != '')//!! { switch(tmp) { case 'W' : rat.Walk(fin.peek() == 'W'); break; case 'L' : rat.TurnLeft(); break; case 'R' : if(fin.get() == 'R') rat.TurnBack(); else { fin.unget(); rat.TurnRight(); } break; case ' ' ://!! rat.TurnBack(); rat.ClearCurrentRoom(); break; } } rat.ClearCurrentRoom(); maze_coord top_left = maze.begin()->first; maze_coord bottom_right = (--maze.end())->first; //This style works, but I guess it's slower #if 0 fout << "Case #" << dec << cur_case << ':' << endl; for(int row = top_left.j; row <= bottom_right.j; row++) { for(int col = top_left.i; col <= bottom_right.i; col++) { fout << hex << (unsigned int)(0x0f&maze[maze_coord(col, row)]); } fout << endl; } #endif //Start writing the file fout << "Case #" << dec << cur_case << ':' << endl; //in non-debug mode, show the progress #ifndef MY_DEBUG cout << "Case #" << dec << cur_case << endl; #endif int max_col = (bottom_right - top_left).i; int cur_col = 0; for(Maze::iterator it = maze.begin(); it != maze.end(); ++it) { #ifdef MY_DEBUG cout << hex << (unsigned int)(*it).second << ' '; #endif fout << hex << (unsigned int)(0x0f&(*it).second); if(cur_col == max_col) { fout << endl; #ifdef MY_DEBUG cout << endl; #endif cur_col = 0; continue; } ++cur_col; } } return 0; }