题目大意
判断给定 (n) 个矩形将平面分成了几个区域。
(1 leq n leq 50) 坐标 (x,y) 均满足 (0 leq x,y leq 10^6)
思路
注意到 (n) 很小
所以我们直接离散后暴力给坐标系加数表示矩阵的覆盖和重叠
答案就是连通块的个数
注意细节
(Code)
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n , m , len1 , len2 , cnt , tot;
int a[55][55] , X[105] , Y[105] , mp[105][105] , used[105][105];
int fx[4][2] = {{0 , 1} , {1 , 0} , {0 , -1} , {-1 , 0}};
struct segment{
int l , t , r , b;
}e[55];
void dfs(int x , int y)
{
if (used[x][y]) return;
used[x][y] = 1;
for(register int i = 0; i < 4; i++)
{
int xx = x + fx[i][0] , yy = y + fx[i][1];
if (xx < 0 || xx > len1 + 1 || yy < 0 || yy > len2 + 1 || mp[xx][yy] != mp[x][y] || used[xx][yy]) continue;
dfs(xx , yy);
}
}
int main()
{
freopen("regions.in" , "r" , stdin);
freopen("regions.out" , "w" , stdout);
scanf("%d" , &n);
for(register int i = 1; i <= n; i++)
{
scanf("%d%d%d%d" , &e[i].l , &e[i].t , &e[i].r , &e[i].b);
X[i] = e[i].l , X[i + n] = e[i].r;
Y[i] = e[i].t , Y[i + n] = e[i].b;
}
m = n * 2;
sort(X + 1 , X + m + 1) , sort(Y + 1 , Y + m + 1);
len1 = unique(X + 1 , X + m + 1) - X - 1 , len2 = unique(Y + 1 , Y + m + 1) - Y - 1;
for(register int i = 1; i <= n; i++)
{
int l = lower_bound(X + 1 , X + len1 + 1 , e[i].l) - X;
int r = lower_bound(X + 1 , X + len1 + 1 , e[i].r) - X;
int t = lower_bound(Y + 1 , Y + len2 + 1 , e[i].t) - Y;
int b = lower_bound(Y + 1 , Y + len2 + 1 , e[i].b) - Y;
tot++;
for(register int j = l; j < r; j++)
for(register int k = b; k < t; k++)
mp[j][k] += tot;
}
for(register int i = 0; i <= len1 + 1; i++)
for(register int j = 0; j <= len2 + 1; j++)
if (!used[i][j]) ++cnt , dfs(i , j);
printf("%d" , cnt);
}