Uva221 离散化

对于x而言无法对每个x枚举是否能看到建筑物 但是可以对每个相邻的x区间中枚举来判断是否能看到建筑物

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
struct Building{
    int id;
    double x,y,w,d,h; // 左下角坐标、x方向宽、y方向宽、高度
    bool operator <(const Building& rhs)const{
        return x < rhs.x || (x == rhs.x && y < rhs.y);
    }
}b[maxn + 1];
int n;
double x[maxn * 2 + 1]; // 存储所有的x坐标
// 判断在x坐标上mx处向北看视线是否穿过(不考虑遮挡)建筑i
bool cover(int i,double mx){
    return b[i].x <= mx && b[i].x + b[i].w >= mx;
}
// 判断建筑物i在x=mx处是否可见
bool visible(int i,double mx){
    if(!cover(i,mx)) return false;
    for(int k = 0 ; k < n ; k++)
    // 如果有一座更高的建筑物在南面挡住了视线
    if(b[k].y < b[i].y && b[k].h >= b[i].h && cover(k,mx)) 
        return false;
    return true;
}
int main(){
    int kase = 0;
    while(scanf("%d",&n) == 1 && n){
        for(int i = 0 ; i < n ; i++){
            scanf("%lf%lf%lf%lf%lf",&b[i].x,&b[i].y,&b[i].w,&b[i].d,&b[i].h);
            x[2 * i] = b[i].x,x[2 * i + 1] = b[i].x + b[i].w;
            b[i].id = i + 1;
        }
        sort(b,b + n); // 按题目要求排序建筑物方便输出
        sort(x,x + 2 * n); // 所有的x坐标进行排序
        int m = unique(x,x + n * 2) - x; // 排序后去重,得到m个坐标
        if(kase++) printf("\n");
        printf("For map #%d, the visible buildings are numbered as follows:\n%d",kase,b[0].id);
        for(int i = 1 ; i < n ; i++){ //判断剩下的n-1个建筑的可见情况
            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",b[i].id);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43413621/article/details/88912802