TopcoderArchive
Archive
Algorithm problem

LabyrinthDecoration

Problem statement, definition, constraints, and public examples.

Problem Statement

We are given a labyrinth as a rectangular matrix where '#' characters represent walls and spaces represent empty areas. We want to format it to look nicer using the following idea:
  • Empty areas will remain as spaces.
  • If a wall is neighbored directly above and below by walls (but not to the left or right), it will be represented as '|'.
  • If a wall is neighbored directly to the left and right by walls (but not above or below), it will be represented as '-'.
  • If a wall has no more than one neighboring wall, it will be represented as 'o' (the lowercase letter).
  • If a wall does not fall into one of the previous categories, it will be represented as '+'.
So, for example, if the input labyrinth is:
{"### #",
 "#   #",
 "# # #",
 "    #",
 "#####"}
The result will be:
{"+-o o",
 "|   |",
 "o o |",
 "    |",
 "o---+"}
Given the original labyrinth as a String[], return a String[] of the same dimensions containing the formatted labyrinth.

Definition

Class:
LabyrinthDecoration
Method:
decorate
Parameters:
String[]
Returns:
String[]
Method signature:
String[] decorate(String[] labyrinth)
(be sure your method is public)

Notes

  • Two cells are neighbors if and only if they share a side. This means inner cells have 4 neighbors, border cells have 3 neighbors and corner cells have 2 neighbors.

Constraints

  • labyrinth will have between 2 and 50 elements, inclusive.
  • Each element of labyrinth will have exactly N characters, where N is an integer between 2 and 50, inclusive.
  • Each character of each element of labyrinth will be either ' ' (a space) or '#'.

Examples

  1. {"### #",
     "#   #",
     "# # #",
     "    #",
     "#####"}
    Returns: {"+-o o", "|   |", "o o |", "    |", "o---+" }
    The example from the problem statement.
  2. {"####",
     "####",
     "####",
     "####"}
    Returns: {"++++", "++++", "++++", "++++" }
    It's not necessary for the input to represent a true "labyrinth".
  3. {"#########",
     "# #     #",
     "    ### #",
     "### #   #",
     "#   # ###",
     "# ### #  ",
     "# #     #",
     "#########"}
    Returns: {"+-+-----+", "o o     |", "    +-o |", "+-o |   |", "|   | +-+", "| +-+ o  ", "| |     o", "+-+-----+" }
  4. {"  # ## # # #  # #   ",
     "     ############ # ",
     " #### # # #  # # ###",
     "   # ####### #  ####"}
    Returns: {"  o o+ o o o  o o   ", "     +++++++-++++ o ", " o-+o | | |  | o +++", "   o o+-+-+o o  o+++" }
Back to all problems