TopCoder problem "Hex" used in TCO04 Semifinal 1 (Division I Level One)



Problem Statement

    
 _
/h\_    
\_/ \_
/v\_/ \_
\_/ \_/ \
/v\_/h\_/
\_/ \_/ \
/v\_/ \_/
\_/ \_/ \
  \_/ \_/
    \_/ \
      \_/

Above is a picture of a 4 x 4 Hex game in progress. The board is a 4 x 4 collection of hexagons packed together, with 4 hexagons in each vertical column, and 4 hexagons in each diagonal from upper left to lower right. Two players play against each other. One of the players ('h') tries to make a horizontal chain of adjacent hexagons stretching between the left and right of the board. The other player ('v') tries to make a vertical chain of adjacent hexagons stretching from the bottom to the top of the board.

We can refer to any position on the board by a pair of coordinates giving the diagonal distance and vertical distance from the upper left hexagon. Using these coordinates, the two hexagons marked 'h' are located at (0,0) and at (2,1).

Given the size of the board and a list of all the marked hexagons, we want software that can draw the board using characters as shown above. Create a class Hex that contains a method picture that is given n (the vertical and diagonal size of the board) and marks (a list of all the marked hexagons) and that returns a picture of the board in the format shown above. The return will be a String[] that if printed one String per line in order would produce the picture. Each element of the return should have no trailing spaces, and at least one of the elements should have no leading spaces.

marks will be a String[] in which each element will consist of exactly 3 characters: two digits giving the diagonal and then the vertical coordinate of a hexagon, followed by either 'v' or 'h', the marking of that hexagon.

 

Definition

    
Class:Hex
Method:picture
Parameters:int, String[]
Returns:String[]
Method signature:String[] picture(int n, String[] marks)
(be sure your method is public)
    
 

Constraints

-n will be between 2 and 10 inclusive.
-marks will contain between 0 and 50 elements inclusive.
-Each element of marks will contain exactly 3 characters.
-The first 2 characters in each element of marks will be digits less than n.
-The last character in each element of marks will be 'v' or 'h'.
-No 2 elements of marks will refer to the same hexagon.
 

Examples

0)
    
4
{"00h","21h","01v","03v","02v"}
Returns: 
{ " _",
 "/h\\_",
 "\\_/ \\_",
 "/v\\_/ \\_",
 "\\_/ \\_/ \\",
 "/v\\_/h\\_/",
 "\\_/ \\_/ \\",
 "/v\\_/ \\_/",
 "\\_/ \\_/ \\",
 "  \\_/ \\_/",
 "    \\_/ \\",
 "      \\_/" }
Note that the elements in the returns are shown as string literals, so each backslash character in each String is shown as \\. So, for example, the second element of this return should contain just 4 characters (i.e. its length would be 4). This will print the following 4x4 picture:
 _
/h\_
\_/ \_
/v\_/ \_
\_/ \_/ \
/v\_/h\_/
\_/ \_/ \
/v\_/ \_/
\_/ \_/ \
  \_/ \_/
    \_/ \
      \_/
1)
    
3
{"00v","01v","02v","11h","21h"}
Returns: 
{ " _",
 "/v\\_",
 "\\_/ \\_",
 "/v\\_/ \\",
 "\\_/h\\_/",
 "/v\\_/h\\",
 "\\_/ \\_/",
 "  \\_/ \\",
 "    \\_/" }
This will print the following 3x3 picture:
 _
/v\_
\_/ \_
/v\_/ \
\_/h\_/
/v\_/h\
\_/ \_/
  \_/ \
    \_/

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5882&pm=3054

Writer:

dgoodman

Testers:

PabloGilberto , lbackstrom , Yarin

Problem categories:

Recursion, String Manipulation, String Parsing