TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

CityScape

Problem statement, definition, constraints, and public examples.

Problem Statement

Given an arrangement of buildings in a city, where their heights are given, you are to determine what this arrangement would look like on the horizon from the south.

For example, if you were given the following String[]:

{"18",
 "35"}

This would mean that in this four-block city, the northwest building has a height of 1, the northeast has a height of 8, the southwest has a height of 3, and the southeast has a height of 5.

The horizon from the south would have the following look:

--  
-X
-X
-X
-X
-X
XX
XX
XX

Where X is a building, and - is not.

Write a class CityScape which contains the method draw. This will take a String[] representing the top view of the city. Each position in the String[] will correspond to the height of one building. You are to return the view of the city from the south as a String[]. This should show hyphens (-) where a building is not visible, and capital Xs (X) where a building is visible.

For example, in the above, you would return the following String[].

{"--",
 "-X",
 "-X",
 "-X",
 "-X",
 "-X",
 "XX",
 "XX",
 "XX"}.

There should always be 9 elements in this String[], each representing a single row of the drawing. Element 0 represents the row at height 9, element 1 represents the row at height 8, and so on. Each element should be the same length, and the length may be anywhere from 1 to 30, depending on the size of the original city.

Definition

Class:
CityScape
Method:
draw
Parameters:
String[]
Returns:
String[]
Method signature:
String[] draw(String[] topView)
(be sure your method is public)

Notes

  • It is possible for a building to have a height of 0, (so, essentially, there is no building there.)

Constraints

  • topView will contain between 1 and 30 elements, inclusive.
  • each element of topView will contain between 1 and 30 characters, inclusive.
  • each element of topView will contain the same number of characters.
  • each element of topView will contain only the characters '0', '1', '2', '3', '4', '5', '6', '7', '8', and '9'.

Examples

  1. {"18"
    ,"35"}
    Returns: { "--",  "-X",  "-X",  "-X",  "-X",  "-X",  "XX",  "XX",  "XX" }
    This is the example from above.
  2. {"678917654"}
    Returns: { "---X-----",  "--XX-----",  "-XXX-X---",  "XXXX-XX--",  "XXXX-XXX-",  "XXXX-XXXX",  "XXXX-XXXX",  "XXXX-XXXX",  "XXXXXXXXX" }
    So we have these tall buildings, and I guess someone didn't want to give up their house.
  3. {"1293842347472",
     "1934825837483",
     "9209482048309",
     "4832849249294",
     "3524983295924",
     "3498292924184"}
    Returns: { "XXXXXXXXXXXXX",  "XXXXXXXXXXXXX",  "XXXXXXXXXXXXX",  "XXXXXXXXXXXXX",  "XXXXXXXXXXXXX",  "XXXXXXXXXXXXX",  "XXXXXXXXXXXXX",  "XXXXXXXXXXXXX",  "XXXXXXXXXXXXX" }
    There is a 9 in every north-south column. Therefore, from the side, it looks like a solid block of city.
  4. {"326106200030403002190140",
    "624610061575010123216040",
    "120611502203053100014253",
    "100011202231454712141030",
    "604312015012010070010120",
    "014011244120124130034206",
    "034116110002651001611020",
    "235041060310022001073042",
    "414302046120110004000010",
    "331620041032162311040215",
    "200620032603002010330051",
    "211222310014006000310540",
    "421826045032702323035051",
    "000030117020200530350331",
    "314110030202044433134530",
    "230101001001120324181012",
    "032113046305652011241120",
    "445542332550031435325014",
    "210564351162010441101040",
    "117500113030021011124021",
    "210142271104120032011120",
    "010022026202123061681515",
    "000111510020203042500202",
    "024402036211433113601044",
    "001031063221122221422014",
    "214003173003413230300132",
    "200320160110120110003432",
    "110361011147471440203501"}
    Returns: { "-------------------X----",  "---X---------------X----",  "--XX---XX-XXXX-XX--X----",  "X-XXXX-XXXXXXXXXX-XXX--X",  "X-XXXXXXXXXXXXXXXXXXXXXX",  "XXXXXXXXXXXXXXXXXXXXXXXX",  "XXXXXXXXXXXXXXXXXXXXXXXX",  "XXXXXXXXXXXXXXXXXXXXXXXX",  "XXXXXXXXXXXXXXXXXXXXXXXX" }
    Bright lights, big city...
Back to all problems