首页 > 代码库 > UVa221 Urban Elevations

UVa221 Urban Elevations

暴力枚举判断是否可见。

注意特判几栋房子一起遮挡一栋房子的情况。

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 const int mxn=1010; 8 struct building{ 9     int x,y,w,h;10     int id;11 }a[mxn],b[mxn];12 int cmp(const building c,const building d){13     return (c.x<d.x)||(c.x==d.x && c.y<d.y);14 }15 int n,m;16 bool check(int t){17     int pos=a[t].x;18     int cnt=0;19     for(int i=1;i<=n;i++){20         if(i==t)continue;21         if(a[i].y<a[t].y && a[i].x<=a[t].x && a[i].x+a[i].w>=a[t].x+a[t].w && a[i].h>=a[t].h)return false;22         if(a[i].y<a[t].y && a[i].h>=a[t].h && a[i].x<=pos && a[i].x+a[i].w>=pos)pos=max(pos,a[i].x+a[i].w);23     if(pos<a[t].x+a[t].w)return true;24     return false;25 }26 int ans[mxn],top=0;27 void solve(){28     for(int i=1;i<=n;i++)29         if(check(i))ans[++top]=a[i].id;30     return;31 }32 int main(){33     int i,j;34     int cas=0;35     while(scanf("%d",&n) && n){36         if(cas)printf("\n");37         top=0;38         for(i=1;i<=n;i++){39             scanf("%d%d%d%*d%d",&a[i].x,&a[i].y,&a[i].w,&a[i].h);40             a[i].id=i;41         }42         sort(a+1,a+n+1,cmp);43         solve();44         printf("For map #%d, the visible buildings are numbered as follows:\n",++cas);45         for(i=1;i<top;i++)printf("%d ",ans[i]);46         printf("%d",ans[top]);47         printf("\n");48     }49     return 0;50 }

 

UVa221 Urban Elevations