热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

poj1151==codevs3044矩形面积求并

AtlantisTimeLimit:1000MSMemoryLimit:10000KTotalSubmis
Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21511   Accepted: 8110

Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

Input

The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 The input file is terminated by a line containing a single 0. Don't process it.

Output

For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point. 
Output a blank line after each test case.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00

Source

Mid-Central European Regional Contest 2000
题解:
 题目的意思是给定n个矩形的2n个坐标,求矩形的覆盖面积。如果开一个大的bool数组,将覆盖过的部分更新为true,再从头到尾扫描一遍,在坐标范围比较小的情况下,可以求解。但是如果坐标x,y的取值范围很大,比如[-10^8,10^8],用上面这个方法就不能求解了;而且坐标还有可能是实数,上面的方法就更加不可行了,需要寻找一种新的解法,就是下面要说到的“离散化”。
    注意到要表示一个矩形,只需要知道其2个顶点的坐标就可以了(最左下,最右上)。可以用2个数组x[0...2n-1],y[0...2n-1]记录下矩形Ri的2个坐标(x1,y1),(x2,y2),然后将数组x[0...xn-1],y[0...2n-1]排序,为下一步的扫描线作准备,这就是离散化的思想。这题还可以用线段树做进一步优化,但是这里只介绍离散化的思想。
    看下面这个例子:有2个矩形(1,1),(3,3)和(2,2),(4,4)。如图:
    图中虚线表示扫描线,下一步工作只需要将这2个矩形覆盖过的部分的bool数组的对应位置更新为true,接下去用扫描线从左到右,从上到下扫描一遍,就可以求出矩形覆盖的总面积。
    这个图对应的bool数组的值如下:
    1 1 0                       1 2 3
    1 1 1       <---->       4 5 6
    0 1 1                       7 8 9
AC代码:
注意::::POJ1151上G++要用%.2f才能过!%.2lf WA!
#include
#include

#include

#include

#include

using namespace std;
const int N=101;
const double eps=1e-6;
double ans=0,x[N<<1],y[N<<1],pos[N][4];
bool hash[N<<1][N<<1];
int cmp(const void *a,const void *b){
double *aa=(double *)a;
double *bb=(double *)b;
if(fabs(*aa-*bb)<=eps) return 0;
else if(*aa-*bb>0) return 1;
return -1;
}
int main(){int i,j,k,n,x1,y1,x2,y2,cas=0;
while(scanf("%d",&n)==1){
if(!n) break;
for(ans=k=i=0;i2){
scanf(
"%lf%lf%lf%lf",&pos[i][0],&pos[i][1],&pos[i][2],&pos[i][3]);
x[k]
=pos[i][0];y[k]=pos[i][1];x[k+1]=pos[i][2];y[k+1]=pos[i][3];
}
memset(hash,
0,sizeof hash);
qsort(x,n
<<1,sizeof x[0],cmp);
qsort(y,n
<<1,sizeof y[0],cmp);
for(i=0;i){
for(k=0;fabs(x[k]-pos[i][0])>eps;k++); x1=k;
for(k=0;fabs(y[k]-pos[i][1])>eps;k++); y1=k;
for(k=0;fabs(x[k]-pos[i][2])>eps;k++); x2=k;
for(k=0;fabs(y[k]-pos[i][3])>eps;k++); y2=k;
for(j=x1;j){
for(k=y1;k){
hash[j][k]=1;
}
}
}
for(i=0;i<2*n-1;i++){
for(j=0;j<2*n-1;j++){
ans
+=hash[i][j]*(x[i+1]-x[i])*(y[j+1]-y[j]);
}
}
printf(
"Test case #%d\n",++cas);
printf(
"Total explored area: %.2lf\n\n",ans);
}
return 0;
}

 


推荐阅读
author-avatar
zzzzzzzzssss
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有