Lab Assignment 10

  1. [solve]

    The skyline problem

    Imagine a collection of buildings in a two-dimensional city. Each building is rectangular. It has a horizontal extent (from one integer to another) and a height.

    You would like to know what the skyline looks like, defined by the tallest building at each position.

    The input for this problem is in a file called skyline.txt. Each line describes the two-dimensional profile of a building, giving three integers: the left end, the right end and the height. For example, line

      14 22 105
    
    describes a building that goes from position 14 to position 22 and has height 105. All of the integers in the file are greater than or equal to 0 and less than 1000.

    The end of the input is marked by a line that contains

      0  0  0
    

    The assignment

    Write a Java program that reads the contents of file skyline.txt and prints the skyline profile. It should print a sequence of lines, each giving a left endpoint, a right endpoint, and a height, indicating a part of the skyline. Two lines in a row must have different heights. The sections must be in left-to-right order. Any place where there is not a building has height 0.

    For example, file

      10 20 19
      15 30 25
      5  15 4
      11 16 8
      35 40 36
      0  0  0
    
    indicates that there is a building from 10 to 20 of height 19, another from 15 to 30 of height 25, etc., as follows.

    The program should display

        0    5    0
        5   10    4
       10   15   19
       15   30   25
       30   35    0
       35   40   36
       45 1000    0
    
    indicating that from 0 to 5 there are no buildings, from 5 to 10 the skyline has height 4, from 10 to 15 the height is 19, etc.

    An algorithm

    Create an array of all of the horizontal positions, holding the skyline height at each position. If your array is called skyline, then skyline[0] holds the height between 0 and 1, skyline[1] holds the skyline height between 1 and 2, etc. Since there are 1000 different horizontal positions, the array should have 1000 values in it.

    Now read the lines describing buildings, one at a time. For each building, insert it into the skyline by looping through the array, possibly storing new heights. For example, if the building goes from horizontal position 102 to 108, then update the heights at locations 102, 103, 104, 105, 106 and 107. Only increase heights. If you encounter a height that is already larger than the height of this building, do not change it. A short building does not lower the skyline.

    After filling the array with heights from the buildings, scan it, looking for places where the height changes. After you have found a section of constant height, print out information about that section, and then get the next section.

    Submitting your work

    Test your program. Look at what it produces. Are the results correct?

    When you are satisfied that it works, paste it into the box below and push the submit button.