Friday, November 13, 2009

Always turn left

I spend some time solving this problem found at the Google code jam website.

Problem statement:
You find yourself standing outside of a perfect maze. A maze is defined as "perfect" if it meets the following conditions:

  1. It is a rectangular grid of rooms, R rows by C columns.
  1. 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.
  1. There is exactly one path between any two rooms in the maze (that is, exactly one path that does not involve backtracking).
entrance_to_exit exit_to_entrance
Character Can walk north? Can walk south? Can walk west? Can walk east?


Case #1:
Case #2:
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.
The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as
All paths will be at least two characters long, consist only of the characters 'W', 'L', and 'R', and begin and end with 'W'.
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 ≤ 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.

I realized that the alien language problem is actually a simple puzzle. Below is the program I wrote to solve it. Sorry for not including any comments. So lazy!

Wednesday, November 11, 2009

The Alien Numbers

The decimal numeral system is composed of ten digits, which we represent as "0123456789" (the digits in a system are written from lowest to highest). Imagine you have discovered an alien numeral system composed of some number of digits, which may or may not be the same as those used in decimal. For example, if the alien numeral system were represented as "oF8", then the numbers one through ten would be (F, 8, Fo, FF, F8, 8o, 8F, 88, Foo, FoF). We would like to be able to work with numbers in arbitrary alien systems. More generally, we want to be able to convert an arbitrary number that's written in one alien system into a second alien system.
The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as
alien_number source_language target_language


9 0123456789 oF8
Foo oF8 0123456789
13 0123456789abcdef 01
Case #1: Foo
Case #2: 9
Case #3: 10011
Case #4: JAM!
Each language will be represented by a list of its digits, ordered from lowest to highest value. No digit will be repeated in any representation, all digits in the alien number will be present in the source language, and the first digit of the alien number will not be the lowest valued digit of the source language (in other words, the alien numbers have no leading zeroes). Each digit will either be a number 0-9, an uppercase or lowercase letter, or one of the following symbols !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
For each test case, output one line containing "Case #x: " followed by the alien number translated from the source language to the target language.
1 ≤ N ≤ 100.
Small dataset

1 ≤ num digits in alien_number ≤ 4,

2 ≤ num digits in source_language ≤ 16,
2 ≤ num digits in target_language ≤ 16.

Large dataset

1 ≤ alien_number (in decimal) ≤ 1000000000,

2 ≤ num digits in source_language ≤ 94,
2 ≤ num digits in target_language ≤ 94.


I prefered c/c++  because I used  to work with them. You can see my solution below. I used microsoft visual c++ 6 to compile this program.You should download the input files from the codejam website.