问题:
在w*h的格子上画了n条垂直或者水平的宽度为1的直线。求出这些直线将格子划分为了多少个区域?
限制条件:
1<= w,h <= 1000000
1<= n <= 500
输入:首先输入w,h,n;然后输入对应的x1,x2,y1,y2.输出区域的个数。
输入:
10 10 5
x1:1 1 4 9 10
x2:6 10 4 9 10
y1:4 8 1 1 6
y2:4 8 10 5 10
输出:
6
分析:
我们可以用一个数组表示所有的格子,然后将格子分为直线上的和不在直线上的,然后进行BFS搜索。但是由于w,h很大,没办法开那么大的数组。所以我们要利用坐标离散化得技巧。
如输入样例所示:
坐标离散化的主要思想是:将前后没有变化的行列消除后并不影响区域的个数
数组里只需要存储有直线的行列和前后的行列就足够了,这样的话大小最多为6n*6n了。
然后在利用BFS搜索即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 500;
int W,H,N;
int X1[maxn],X2[maxn],Y1[maxn],Y2[maxn];
bool fld[maxn*6][maxn*6];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
//对x1和x2进行坐标离散化,并返回离散后的宽度。(对于y1,y2同理)
//将x1,x2更新为离散后的x1,x2.y不变在x方向上缩小。(处理y1,y2时同理)
int compress(int *x1,int *x2,int w)
{
vector<int> xs;
for(int i = 0;i < N;i++)//确定离散后x轴上哪些值还存在
{
for(int d = -1;d <= 1; d++)
{
int tx1 = x1[i] + d, tx2 = x2[i] + d;
if(1 <= tx1 && tx1 <= w) xs.push_back(tx1);
if(1 <= tx2 && tx2 <= W) xs.push_back(tx2);
}
}
sort(xs.begin(),xs.end());
xs.erase(unique(xs.begin(),xs.end()),xs.end());//去重
for(int i = 0; i < N; i++)//转化为新的x1,x2;
{
x1[i] = find(xs.begin(),xs.end(),x1[i])-xs.begin();
x2[i] = find(xs.begin(),xs.end(),x2[i])-xs.begin();
}
return xs.size();
}
void solve()
{
//离散化
W = compress(X1,X2,W);
H = compress(Y1,Y2,H);
//填充新的网格
memset(fld,0,sizeof(fld));
for(int i=0;i<N;i++)
{
for(int y=Y1[i];y<=Y2[i];y++)
{
for(int x=X1[i];x<=X2[i];x++)
{
fld[y][x]=true;
}
}
}
//利用BFS计算区域数
int ans=0;
for(int y=0;y<H;y++)
{
for(int x=0;x<W;x++)
{
if(fld[y][x]) continue;
ans++;
queue<pair<int, int> > que;
que.push(make_pair(x,y));
while(!que.empty())
{
int sx=que.front().first, sy=que.front().second;
que.pop();
for(int i=0;i<4;i++)
{
int tx=sx + dx[i],ty=sy + dy[i];
if(tx<0 || tx>=W || ty<0 || ty>=H || fld[ty][tx]) continue;
que.push(make_pair(tx,ty));
fld[ty][tx]=true;
}
}
}
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%d%d",&W,&H,&N)==3)
{
for(int i=0;i<N;i++)
scanf("%d",&X1[i]);
for(int i=0;i<N;i++)
scanf("%d",&X2[i]);
for(int i=0;i<N;i++)
scanf("%d",&Y1[i]);
for(int i=0;i<N;i++)
scanf("%d",&Y2[i]);
solve();
}
return 0;
}
/*
输入:
10 10 5
1 1 4 9 10
6 10 4 9 10
4 8 1 1 6
4 8 10 5 10
输出:
6
*/