Description
在一个平面上有n个矩形。每个矩形的边都平行于坐标轴并且都具有值为整数的顶点。我们用如下的方式来定义块。
每一个矩形都是一个块。
如果两个不同的矩形有公共线段,那么它们就组成了一个新的块来覆盖它们原来的两个块。
例子:
在图1中的矩形组成了两个不同的块。
写一个程序:
从文件PRO.IN中读入矩形的个数以及它们的顶点。
找出这些矩形形成的不同的块的个数。
将结果写入文件PRO.OUT。
Input
在输入文件PRO.IN的第一行又一个整数n,1 <= n <=7000,表示矩形的个数。接下来的n行描述矩形的顶点,每个矩形用四个数来描述:左下顶点坐标(x,y)与右上顶点坐标(x,y)。每个矩形的坐标都是不超过10000的非负整数。
Output
在文件PRO.OUT的第一行应当仅有一个整数---表示由给定矩形组成的不同的块的个数。
Sample Input
9
0 3 2 6
4 5 5 7
4 2 6 4
2 0 3 2
5 3 6 4
3 2 5 3
1 4 4 7
0 0 1 4
0 0 4 1
Sample Output
2
解题思路:先读入数据,用二维数组储存,用并差集的思想,如果两个图形有重合或者是贴合在一起,就更改它的编号,最后输出编号未被更改过的个数即可。
程序:
var
a:array[1..10000,1..4] of longint;
p:array[1..10000] of longint;
n,i,j,t1,t2,ans:longint;
function find(x:longint):longint;
var
y:longint;
begin
y:=x;
while p[y]<>0 do
y:=p[y];
find:=y;
end;
function check(i,j:longint):boolean;
begin
if (a[i,3]
if (a[i,4]
if ((a[i,3]=a[j,1])or(a[j,3]=a[i,1]))and((a[i,4]=a[j,2])or(a[j,4]=a[i,2])) then exit(false);
exit(true);
end;
begin
readln(n);
for i:=1 to n do
for j:=1 to 4 do
read(a[i,j]);
for i:=1 to n-1 do
begin
t1:=find(i);
for j:=i+1 to n do
if check(i,j) then
begin
t2:=find(j);
if t1<>t2 then p[t2]:=t1;
end;
end;
for i:=1 to n do
if p[i]=0 then inc(ans);
writeln(ans);
end.
版权属于:Chris
原文地址:http://blog.sina.com.cn/s/blog_83ac6af80102v444.html
转载时必须以链接形式注明原始出处及本声明。