TopCoder problem "RoadTrip" used in TCI '02 Semifinals 1 (Division I Level One)



Problem Statement

    You are going on a road trip with your family. Your dad has given you a map, and has marked where the family car will have to make turns on it. For example:
String[] map = {".........R....",
		"..............",
		"...L.....R....",
		"..............",
		"...L.....B...."}
'.' denotes open road, 'R' denotes right turn, 'L' denotes left turn, and 'B' denotes turn around

You will keep travelling until you leave the map boundaries or loop forever. Assuming you started in the top left hand corner travelling right, in the above example your path would be:
 >>>>>>>>>v....                                 ..............
 .........v....                                 ..............
 ...v<<<<<<....   at this point you hit the B   ..............
 ...v..........   and need to turn around so:   ..............
 ...>>>>>><....                                 ...v<<<<<<....
Note that the turns are always relative to the direction you are going.



Your family has decided that it wants to take the most scenic path possible, in other words, travel to the most number of unique positions on the map. In the above example, starting at the upper left hand corner heading right allowed you to cover these locations:
 xxxxxxxxxx....                 
 .........x....
 ...xxxxxxx.... marks off all of the squares
 ...x.......... the car had traveled to.
 ...xxxxxxx....
The total number of x's (unique locations) is 26. Your job is to find the spot and direction on the map you would have to begin at in order to produce the most scenic trip (greatest number of unique locations touched). You will return the number of unique locations touched on the most scenic trip. In the example above, the trip given is the most scenic one, and thus your method would return 26.

If there is a turn symbol in the position where you start it comes into effect immediately. For example:

map = {"R...B"}

If you started at the leftmost point facing right your path would look like:

v....


If you started at the rightmost point facing right your path would look like:

^<<<<


For this example your method would return 5.

A word of caution: loops can occur in the paths chosen. Regardless of whether there is a loop or not, you will always return the number of unique squares you will touch.
 

Definition

    
Class:RoadTrip
Method:howMany
Parameters:String[]
Returns:int
Method signature:int howMany(String[] map)
(be sure your method is public)
    
 

Notes

-The point you choose must be ON the map, and the directions must be one of the four directions: Up, Down, Left, Right
 

Constraints

-map must contain between 1 and 30 elements inclusive
-Each element of map will have length between 1 and 30 inclusive
-Each element of map will have the same length
-Each element of map will only consist of letter from the following string (quotes for clarity): "BRL."
 

Examples

0)
    
{".........R....",
 "..............",
 "...L.....R....",
 "..............",
 "...L.....B...."}
Returns: 26
The first example from the problem statement. Heading right from the top left corner is optimal.
1)
    
{".........R.....R.......",
 ".......................",
 ".........B.............",
 ".......................",
 ".......................",
 "..B......R.....R......B",
 ".......................",
 ".......................",
 ".......................",
 ".........B.....B......."}
Returns: 60
2)
    
{"......................R",
 "R....................R.",
 ".R..................R..",
 "..R................R...",
 "...R..............R....",
 "....R.............R....",
 "...R...............R...",
 "..R.................R..",
 ".R...................R.",
 "R.....................R"}
Returns: 230
Starting right from the top left corner spirals arounds the board
3)
    
{".B.....B"}
Returns: 7
Watch out for loops
4)
    
{"..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 "..............................",
 ".............................."}
Returns: 30

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=1165

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4370&pm=1165

Writer:

brett1479

Testers:

alexcchan , lbackstrom

Problem categories:

Brute Force, Simulation