您的位置:首页 > 编程语言 > C语言/C++

The Skyline Problem轮廓线问题算法详解

2017-06-02 15:18 627 查看

问题详见: The Skyline Problem

题目让我们求解出给定建筑物的轮廓线的关键点。题目描述如下:

A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).





The geometric information of each building is represented by a triplet of integers [Li,Ri,Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0≤Li, Ri≤INTMAX, 0<Hi≤INTMAX, and Ri−Li>0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as: [[2910],[3715],[51212],[152010],[19248]].

The output is a list of “key points” (red dots in Figure B) in the format of [[x1,y1],[x2,y2],[x3,y3],...] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[[210],[315],[712],[120],[1510],[208],[24,0]].

Notes:

The number of buildings in any input list is guaranteed to be in the range [0,10000].

The input list is already sorted in ascending order by the left x position Li.

The output list must be sorted by the x position.

There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[23],[45],[75],[115],[127]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[23],[45],[127],...]

解题思路:

由于题目中建筑物默认都是规则的矩形,求解的轮廓线的关键点是当前扫描且未扫描完成的建筑物的边缘角的坐标,所以考虑使用队列来储存当前扫描且未扫描完成的建筑物信息。当前扫描到的建筑物要么是全新的(即和之前扫描的建筑物没有任何交集),要么是和队列中的建筑物存在交集,此时只需根据新扫描的建筑物信息更新队列和轮廓线边缘角坐标即可。整个算法最差情况复杂度为O(nlog(n))。具体算法如下:

class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int>> res;
int cur=0, cur_X=0, cur_H=0, len=buildings.size();
priority_queue<pair<int, int>> liveBlg;
while(cur<len || !liveBlg.empty())  {
cur_X = liveBlg.empty()? buildings[cur][0]:liveBlg.top().second;
if(cur>=len || buildings[cur][0] > cur_X)
while(!liveBlg.empty() && (liveBlg.top().second <= cur_X) )
liveBlg.pop();
else{
cur_X = buildings[cur][0];
while(cur<len && buildings[cur][0]== cur_X) {
liveBlg.push(make_pair(buildings[cur][2], buildings[cur][1]));
cur++;
}
}
cur_H = liveBlg.empty()?0:liveBlg.top().first;
if(res.empty() || (res.back().second != cur_H) ) res.push_back(make_pair(cur_X, cur_H));
}
return res;
}
};


其提交运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 C++