在一个笛卡尔平面坐标系里(则X轴向右是正方向,Y轴向上是正方向),有N(1 <= N <= 10)个矩形,第i个矩形的左上角坐标是(x1, y1),右下角坐标是(x2,y2)。问这N个矩形所覆盖的面积是多少?注意:被重复覆盖的区域的面积只算一次。

输入格式:

第一行,一个整数N。 (1 <= N <= 10)。

接下来有N行,每行描述一个矩形的信息,分别是矩形的x1、y1、x2、y2。

其中 -10^4 <= x1,y1,x2,y2 <= 10^4。

输出格式:

一个整数,被N个矩形覆盖的区域的面积。

输入样例:planting.in

2

0 5 4 1

2 4 6 2

输出样例:planting.out

20

分析:

由于数据很小,所以必然模拟,我做的时候一直没有想到怎样去处理重叠部分,怎样去判断,之后看了标程才明白。将所有的横纵坐标分别从小到大排序,每次都找到构成的小矩形,也就是把所有分出来的小矩形都枚举一边,这样既能计算面积,又不会存在重复计算  

code:

View Code
program second;
var
n,i,j,k,tot,ans : longint;
a,b : array[1..50] of longint;
x,y : array[1..20,1..10] of longint;
procedure init;
var
t : longint;
begin
tot:=0;
readln(n);
for i:=1 to n do
begin
readln(x[i,1],y[i,1],x[i,2],y[i,2]);
a[i+i-1]:=x[i,1];
b[i+i-1]:=y[i,1];
a[i+i]:=x[i,2];
b[i+i]:=y[i,2];
end;
tot:=2*n;
for i:=1 to tot-1 do
for j:=i+1 to tot do
if a[i]>a[j] then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
end;
for i:=1 to tot-1 do
for j:=i+1 to tot do
if b[i]>b[j] then
begin
t:=b[i];
b[i]:=b[j];
b[j]:=t;
end;
end; { init }
procedure work;
var
flag : boolean;
begin
for i:=1 to tot-1 do
for J:=1 to tot-1 do
begin
flag:=false;
for k:=1 to n do
if (a[i]>=x[k,1]) and (b[j]>=y[k,2]) and (a[i+1]<=x[k,2]) and (b[j+1]<=y[k,1]) then
flag:=true;
if flag then
inc(ans,(a[i+1]-a[i])*(b[j+1]-b[j]));
end;
end; { work }
begin
assign(input,'planting.in'); reset(input);
assign(output,'planting.out'); rewrite(output);
init;
work;
writeln(ans);
close(input);
close(output);
end.



posted on 2012-03-28 17:41  淡·雅·墨  阅读(923)  评论(0编辑  收藏  举报