uva221-Urban Elevations

来源:互联网 发布:淘宝客下载官方网站 编辑:程序博客网 时间:2024/06/01 22:37

题意:输出从正视图能看到的建筑物的id。

思路:离散化,把无穷化为有限。把所有x坐标排序去重,则任意两个相邻x坐标形成的区间具有相同属性。只需要在这个区间里任选一个点例如中点,就能判断出这个建筑物在这个区间里是否可见。建筑物的坐标包含这个x坐标,南边不能存在一个建筑物也包含这个坐标并且比它高。

代码:

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>using namespace std;struct Building{  int id;  double x, y, width, depth, height;  bool operator < (const Building& b) {    return x < b.x ||(x == b.x && y < b.y);  }}no[2000+10];double x[210];int n;bool cover(int i, double mx) {return no[i].x  <= mx && no[i].x + no[i].width >= mx;}bool visible(int i, double mx) {if (!cover(i, mx)) return false;for (int k=0; k<n; k++) {    if (no[k].y < no[i].y && no[k].height >= no[i].height && cover(k, mx)) return false;}return true;}int main(){    int  times = 0;    while (scanf("%d", &n) == 1 && n) {        for (int i = 0; i<n; i++) {            scanf("%lf%lf%lf%lf%lf", &no[i].x, &no[i].y, &no[i].width, &no[i].depth, &no[i].height);            x[i*2] = no[i].x; x[i*2+1] = no[i].x + no[i].width;            no[i].id = i + 1;         }        sort(no, no+n);        sort(x, x+n*2);        int m = unique(x, x+n*2) - x; //x坐标排序后去重得到m个坐标        if (times) puts("");        printf("For map #%d, the visible buildings are numbered as follows:\n%d", ++times, no[0].id);        for (int i=1; i<n; i++) {            bool vis = false;            for (int j=0; j<m-1; j++) {               if (visible(i, (x[j]+x[j+1])/2)) { vis = true; break;}            }           if(vis) printf(" %d", no[i].id);        }    puts("");    }    return 0;}

         


0 0
原创粉丝点击