Note that there are some explanatory texts on larger screens.

plurals
  1. POThe Skyline Problem‍​​
    text
    copied!<p>I just came across this little problem on <a href="http://uva.onlinejudge.org/index.php?option=com_onlinejudge&amp;Itemid=8&amp;category=3&amp;page=show_problem&amp;problem=41" rel="nofollow noreferrer">UVA's Online Judge</a> and thought, that it may be a good candidate for a little code-golf.</p> <p><strong>The problem:</strong></p> <p>You are to design a program to assist an architect in drawing the skyline of a city given the locations of the buildings in the city. To make the problem tractable, all buildings are rectangular in shape and they share a common bottom (the city they are built in is very flat). The city is also viewed as two-dimensional. A building is specified by an ordered triple <strong>(Li, Hi, Ri)</strong> where <strong>Li</strong> and <strong>Ri</strong> are left and right coordinates, respectively, of building i and <strong>Hi</strong> is the height of the building. </p> <p><img src="https://imgur.com/SnSjn.gif" alt="alt text"></p> <p>In the diagram below buildings are shown on the left with triples </p> <pre><code>(1,11,5), (2,6,7), (3,13,9), (12,7,16), (14,3,25), (19,18,22), (23,13,29), (24,4,28) </code></pre> <p>and the skyline, shown on the right, is represented by the sequence: </p> <pre><code>1, 11, 3, 13, 9, 0, 12, 7, 16, 3, 19, 18, 22, 3, 23, 13, 29, 0 </code></pre> <p>The output should consist of the vector that describes the skyline as shown in the example above. In the skyline vector <strong>(v1, v2, v3, ... vn)</strong> , the <strong>vi</strong> such that i is an even number represent a horizontal line (height). The <strong>vi</strong> such that i is an odd number represent a vertical line (x-coordinate). The skyline vector should represent the "path" taken, for example, by a bug starting at the minimum x-coordinate and traveling horizontally and vertically over all the lines that define the skyline. Thus the last entry in the skyline vector will be a 0. The coordinates must be separated by a blank space.</p> <p>If I will not count declaration of provided (test) buildings and including all spaces and tab characters, my solution, in Python, is <strong>223</strong> characters long.</p> <p>Here is the condensed version:</p> <pre><code>B=[[1,11,5],[2,6,7],[3,13,9],[12,7,16],[14,3,25],[19,18,22],[23,13,29],[24,4,28]] # Solution. R=range v=[0 for e in R(max([y[2] for y in B])+1)] for b in B: for x in R(b[0], b[2]): if b[1]&gt;v[x]: v[x]=b[1] p=1 k=0 for x in R(len(v)): V=v[x] if p and V==0: continue elif V!=k: p=0 print "%s %s" % (str(x), str(V)), k=V </code></pre> <p>I think that I didn't made any mistake but if so - feel free to criticize me.</p> <p>I don't have much reputation, so I will pay only 100 for a bounty - I am curious, if anyone could try to solve this in less than .. lets say, 80 characters. Solution posted by <strong>cobbal</strong> is <a href="https://stackoverflow.com/questions/1066234/the-skyline-problem/1073337#1073337">101 characters long</a> and currently it is the best one.</p> <p>I thought, that 80 characters is a sick limit for this kind of problem. <strong>cobbal</strong>, with his 46 character solution totaly amazed me - though I must admit, that I spent some time reading his explanation before I partially understood what he had written.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload