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.
City map. Boldface numbers (in the upper left of each building) identify the buildings. Plain numbers (lower right) are the buildings heights. Southern Elevation (Only the shaded 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 #1, map #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 m are 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坐标优先,然后再比较y),然后将矩形的所有x坐标排序并去重,构造出每个小区间,易知每个矩形在这个小区间内要么可见,要么不可见,所以可以按照前面排好的顺序依次检查每个矩形,对于每个矩形依次检查每个小区间,在每个小区间内依次检查是存在其他矩形也在这个小区间内,且在其前面并且高度不比其低
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
/************************************************** filename :g.cpp author :maksyuki created time :2018/1/19 10:10:31 last modified :2018/1/19 10:35:55 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 166; struct node { int id; double x, y, w, d, h; bool operator < (const node &b) const { return x < b.x || (x == b.x && y < b.y); } }; node aa[maxn]; double x[maxn*2]; int n; bool check(double x1, double x2, double mx) { if(mx >= x1 && mx <= x2) return true; return false; } bool visible(int p, double mx) { if(!check(aa[p].x, aa[p].x + aa[p].w, mx)) return false; for(int i = 1; i <= n; i++) if(check(aa[i].x, aa[i].x + aa[i].w, mx) && aa[i].y < aa[p].y && aa[i].h >= aa[p].h) return false; return true; } int main() { #ifdef LOCAL CFF; //CFO; #endif int id = 1; while(scanf(" %d", &n) != EOF) { if(!n) break; for(int i = 1; i <= n; i++) { scanf(" %lf %lf %lf %lf %lf", &aa[i].x, &aa[i].y, &aa[i].w, &aa[i].d, &aa[i].h); aa[i].id = i; x[i*2-1] = aa[i].x, x[i*2] = aa[i].x + aa[i].w; } sort(aa + 1, aa + 1 + n); sort(x + 1, x + 1 + 2 * n); int len = unique(x + 1, x + 1 + 2 * n) - (x + 1); len++; if(id > 1) puts(""); printf("For map #%d, the visible buildings are numbered as follows:\n", id++); printf("%d", aa[1].id); for(int i = 2; i <= n; i++) { bool is_visible = false; for(int j = 1; j <= len - 1; j++) if(visible(i, (x[j] + x[j+1]) / 2.0)) { is_visible = true; break; } if(is_visible) printf(" %d", aa[i].id); } puts(""); } return 0; } |