ZOJ 1128 Atlantis(几何数学)

Atlantis

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 file 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<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don��t process it1.


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 

  在平面上有很多矩形,矩形的两边与坐标轴平行,矩形有可能相互重叠,计算矩形在平面上占用的面积和(几重叠部分职能计算一次)

  作所有矩形的包络矩形(即包围所有矩形的最小矩形),延伸所有矩形的两边与包络矩形相交,构成一个网络。用逻辑值表示网络被矩形覆盖的情况,如果某一个网格被矩形覆盖逻辑值为1,否则为0.统计所有逻辑值为1的网格面积即可。

  可以利用线段树做,留着以后好好练习下。 这里的double类型快排要注意下。

代码如下:

 

 1 # include<stdio.h>
 2 # include<stdlib.h>
 3 # include<memory.h>
 4 # include<math.h>
 5 
 6 struct
 7 {
 8     double x1,y1,x2,y2;
 9 } rect[102];        //所有矩形的坐标
10 double x[202],y[202];       //网格的坐标
11 char valid[202][202];       //网格被矩形覆盖的情况
12 int n;          //矩形的数量
13 
14 //排序因子,按坐标升序
15 int cmp(const void *a,const void *b)
16 {
17     double temp = *(double *)a - *(double *)b;
18     if(fabs(temp)<1e-5) return 0;
19     else if(temp>0) return 1;
20     else return -1;
21 }
22 
23 //计算网格被矩形覆盖的情况
24 void solve()
25 {
26     int i,j,i1,j1;
27     int left,top,right,bottom;
28     //对每一个矩形,将其覆盖的网格单元全部置1
29     for(i=0; i<n; i++)
30     {
31         //找到第i个矩形左边的坐标标号
32         j=0;
33         while(fabs(x[j]-rect[i].x1)>0 && j<2*n) j++;
34         left = j;
35         //找到第i个矩形顶部的坐标标号
36         j=0;
37         while(fabs(y[j]-rect[i].y1)>0 && j<2*n) j++;
38         top = j;
39         //右边的坐标
40         j=0;
41         while(fabs(x[j]-rect[i].x2)>0 && j<2*n) j++;
42         right = j;
43         //底部的坐标
44         j=0;
45         while(fabs(y[j]-rect[i].y2)>0 && j<2*n) j++;
46         bottom = j;
47         //将该矩形所覆盖的网格全部置1
48         for(i1=left; i1<right; i1++)
49             for(j1=top; j1<bottom; j1++)
50                 valid[i1][j1] = 1;
51     }
52 }
53 
54 int main()
55 {
56     int i,j;
57     int iCase = 1;
58     while(scanf("%d",&n) && n)
59     {
60         //读取矩形的数据,并构建网格坐标
61         j=0;
62         for(i=0; i<n; i++)
63         {
64             scanf("%lf%lf%lf%lf",&rect[i].x1,&rect[i].y1,&rect[i].x2,&rect[i].y2);
65             x[j] = rect[i].x1;
66             y[j] = rect[i].y1;
67             j++;
68             x[j] = rect[i].x2;
69             y[j] = rect[i].y2;
70             j++;
71         }
72         qsort(x,2*n,sizeof(double),cmp);
73         qsort(y,2*n,sizeof(double),cmp);
74         memset(valid,0,sizeof(valid));
75         solve();
76         double sum=0;
77         //统计面积,累加被矩形覆盖的网格面积
78         for(i=0; i<2*n; i++)
79             for(j=0; j<2*n; j++)
80                 sum += valid[i][j]*(x[i+1]-x[i])*(y[j+1]-y[j]);
81         printf("Test case #%d\n",iCase++);
82         printf("Total explored area: %.2f\n\n",sum);
83     }
84     return 0;
85 }
View Code

这里的标记为1,是标记矩形的左上角。之前构成了一个网络,网络里有许许多多的矩形,这里的标记的左上角对应的矩形就是以这个点为矩形左上角的那个最小的矩形。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值