码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 221(区间覆盖)

时间:2014-11-20 20:20:20      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

 Urban Elevations 

An elevation of a collection of buildings is an orthogonal projection of the buildings onto a vertical plane. An external elevation of a city would show the skyline and the faces of the ``visible" buildings of the city as viewed from outside the city from a certain direction. A southern elevation shows no sides; it shows the perfectly rectangular faces of buildings or parts of faces of buildings not obstructed on the south by taller buildings. For this problem, you must write a program that determines which buildings of a city are visible in a southern elevation.

 

For simplicity, assume all the buildings for the elevation are perfect rectangular solids, each with two sides that run directly east-west and two running directly north-south. Your program will find the buildings that appear in a southern elevation based on knowing the positions and heights of each city building. That data can be illustrated by a map of the city as in the diagram on the left below. The southern elevation for that city is illustrated in the diagram on the right.

 

bubuko.com,布布扣(The shadow buildings are visible in a southern elevation)

 

Input

Input for your program consists of the numeric description of maps of several cities. The first line of each map contains the number of buildings in the city (a non-negative integer less than 101). Each subsequent line of a map contains data for a single building - 5 real numbers separated by spaces in the following order:

 

x-coordinate of the southwest corner

y-coordinate of the southwest corner

width of the building (length of the south side)

depth of the building (length of the west side)

height of the building

Each map is oriented on a rectangular coordinate system so that the positive x-axis points east and the positive y-axis points north. Assume that all input for each map corresponds to a legitimate map (the number of buildings is the same as the number of subsequent lines of input for the map; no two buildings in a single map overlap). Input is terminated by the number 0 representing a map with no buildings.

 

Output

Buildings are numbered according to where their data lines appear in the map‘s input data - building #1 corresponding to the first line of building data, building #2 data to the next line, and building #n to the nth line of building data for that map. (Buildings on subsequent maps also begin their numbering with 1.)

 

For each map, output begins with line identifying the map (map #1map #2, etc.) On the next line the numbers of the visible buildings as they appear in the southern elevation, ordered south-to-north, west-to-east. This means that if building n and building mare visible buildings and if the southwest corner of building n is west of the southwest corner of building m, then number n is printed before number m. If building n and building m have the same x-coordinate for their southwest corners and if building n is south of building m, then the number n is printed before the number m.

 

For this program, a building is considered visible whenever the part of its southern face that appears in the elevation has strictly positive area. One blank line must separate output from consecutive input records.

 

Sample Input

 

14
160 0 30 60 30
125 0 32 28 60
95 0 27 28 40
70 35 19 55 90
0 0 60 35 80
0 40 29 20 60
35 40 25 45 80
0 67 25 20 50
0 92 90 20 80
95 38 55 12 50
95 60 60 13 30
95 80 45 25 50
165 65 15 15 25
165 85 10 15 35
0

 

Sample Output

 

For map #1, the visible buildings are numbered as follows:
5 9 4 3 10 2 1 14

 

思路很简单,今天自己做了一遍,刚开始的时候,将所有的x映射到x轴上,去除重复的点,那么剩下的就是,每两个相邻的x之间,形成一个区间,那么从这个区间上取一点,不妨为中点(其实哪个点都无所谓,因为这个区间是所有的x映射到x轴上组成的区间),然后判断一下是否这个点被覆盖就行了,只要这个点被覆盖了,那么整个区间就会被覆盖,那么如何知道这个区间属于哪个建筑物呢?(建筑物i(有可能很多)覆盖x就行了)

 

 

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 int n;
10 
11 class Node {
12 public:
13     double x,y,w,d,h;
14     int id;
15     Node (double xx,double yy,double ww,double dd,double hh,int idd):
16         x(xx),y(yy),w(ww),d(dd),h(hh),id(idd){}
17     bool operator < (const Node& rhs) const{
18         return (x == rhs.x ? y < rhs.y : x < rhs.x);
19     }
20 };
21 
22 vector <Node> v;
23 
24 bool cover(int i,double x) {
25     return v[i].x <= x && v[i].x + v[i].w >= x;
26 } 
27 
28 bool visitbal(int i,double x) {
29     if (!cover(i,x)) return 0;
30     for (int j = 0;j < n;j++) {
31         if (v[j].y < v[i].y && v[j].h >= v[i].h && cover(j,x)) return 0;
32     }
33     return 1;
34 }
35 
36 int main () {
37     int Case = 0;
38     // freopen("1.in","r",stdin);
39     while (cin >> n , n) {
40         v.clear();
41         double xx[300];
42         for (int i = 0;i < n;i++) {
43             double x,y,w,d,h;
44             cin >> x >> y >> w >> d >> h;
45             xx[2 * i] = x;xx[2 * i + 1] = x + w;
46             v.push_back(Node(x,y,w,d,h,i + 1));
47         }
48         if (Case++) cout << endl;
49         printf("For map #%d, the visible buildings are numbered as follows:\n",Case);
50         sort(v.begin(),v.end());
51         sort(xx,xx + 2 * n);
52         cout << v[0].id;
53         int m = unique(xx,xx + n * 2) - xx;
54         for (int i = 1;i < n;i++) {
55             int ok = 0;
56             for (int j = 0;j < m - 1;j++) {
57                 if (visitbal(i,(xx[j] + xx[j + 1]) / 2)) {
58                     ok = 1;
59                     break;
60                 }
61             }
62             if (ok) cout << " " << v[i].id;
63         }
64         cout << endl;
65     }
66 }
View Code

 

 

UVA - 221(区间覆盖)

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/xiaoshanshan/p/4111335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!