矩形(并查集)

题意:

• 在平面上画了N个长方形,每个长方形的边平行于坐标轴并且顶点坐标为整数。我们用以下方式定义印版:
– 每个长方形是一个印版;
– 如果两个印版有公共的边或内部,那么它们组成新的印版,否则这些印版是分离的
就是把两个相交的矩形变成一个

思路:

判断他们是否是相交,是的话就用并查集放到一个集合里面去,然后看被分成几个集合就好了

const
 maxn=10000;
var
 f,x1,y1,x2,y2,t:array [1..maxn] of longint;
 i,j,n,ans:longint;

function father(x:longint):longint;
var
 i,j:longint;
begin
 if x<>f[x] then father:=father(f[x])
            else father:=x;
 f[x]:=father;
end;

function cheak(i,j:longint):boolean;
begin
 if (x1[i]>x2[j]) or (x2[i]<x1[j]) then exit(false);
 if (y1[i]>y2[j]) or (y2[i]<y1[j]) then exit(false);
 if (x2[i]=x1[j]) and (y1[i]=y2[j]) then exit(false);
 if (x1[i]=x2[j]) and (y2[i]=y1[j]) then exit(false);
 if (x2[i]=x1[j]) and (y2[i]=y1[j]) then exit(false);
 if (x1[i]=x2[j]) and (y1[i]=y2[j]) then exit(false);
 exit(true);
end;

begin
 readln(n);
 for i:=1 to n do
  readln(x1[i],y1[i],x2[i],y2[i]);
 for i:=1 to n do
  f[i]:=i;

 for i:=1 to n do
  for j:=i+1 to n do
   if cheak(i,j) then
      f[father(i)]:=father(j);

 for i:=1 to n do
  if father(f[i])<>0 then inc(t[father(f[i])]);
 for i:=1 to 10000 do
  if t[i]<>0 then inc(ans);
 writeln(ans);
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值