UVa 221 Urban Elevations

来源:互联网 发布:python是开源的吗 编辑:程序博客网 时间:2024/06/05 18:35

 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.

(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 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 numberm. 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

14160 0 30 60 30125 0 32 28 6095 0 27 28 4070 35 19 55 900 0 60 35 800 40 29 20 6035 40 25 45 800 67 25 20 500 92 90 20 8095 38 55 12 5095 60 60 13 3095 80 45 25 50165 65 15 15 25165 85 10 15 350

Sample Output

For map #1, the visible buildings are numbered as follows:5 9 4 3 10 2 1 14
#include <cstdio>#include <algorithm>using namespace std;typedef struct building{int id;double x, y, width, depth, height;bool operator < (const struct building& b) const{if(x < b.x || (x == b.x && y < b.y))return true;elsereturn false;}}building;building build[200];double x[400];int main(){int n;int m_count = 0;while(scanf("%d", &n) && n != 0){m_count++;if(m_count != 1)printf("\n");printf("For map #%d, the visible buildings are numbered as follows:\n", m_count);int count = 0;while(count < n){scanf("%lf%lf%lf%lf%lf", &build[count].x, &build[count].y, &build[count].width, &build[count].depth,&build[count].height);x[count*2] = build[count].x;x[count*2+1] = build[count].x + build[count].width;build[count].id = count;count++;}sort(build, build+n);sort(x, x+n*2);int x_size = unique(x, x+n*2) - x;printf("%d", build[0].id+1);for(int i = 1; i < n; i++){int j;for(j = 0; j < x_size; j++){double center = (x[j]+x[j+1])/2;if(center >= build[i].x && center <= (build[i].x+build[i].width)){int k;for(k = 0; k < n; k++){if((k != i) && center >= build[k].x && center <= (build[k].x+build[k].width)){if(build[k].y < build[i].y && build[k].height >= build[i].height)break;}}if(k == n){printf(" %d", build[i].id+1);break;}}}}printf("\n");}return 0;}


一开始直接穷举,没有做出来,不知为何。
后来看答案,发现方法很巧,将所有x坐标排序,去掉重复的,任意相邻的区间,对于每个建筑物来说,要么包含,要么不相交,感觉很巧。
还是不明白为什么,以及unique操作,以后再看看。
另外,可以在struct里面定义<符号,这样就可以用sort来排序了。
0 0
原创粉丝点击