AOJ 0531 坐标离散化 + bfs

题意

传送门 AOJ 0531

矩形的宽和高 1 ≤ w, h ≤ 1000000 数据范围大,要离散化处理。对于划分区域,bfs 即可(递归实现可能栈溢出)。填充数组 fld[x][y] 代表左下角顶点坐标离散表示为 (x, y)1 * 1 矩形。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#define min(a,b)    (((a) < (b)) ? (a) : (b))
#define max(a,b)    (((a) > (b)) ? (a) : (b))
#define abs(x)    ((x) < 0 ? -(x) : (x))
#define INF 0x3f3f3f3f
#define delta 0.85
#define eps 1e-3
#define PI 3.14159265358979323846
#define MAX_N 1005
using namespace std;
typedef pair<int, int> P;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
int W, H, N;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
bool fld[MAX_N * 4][MAX_N * 4];
//坐标压缩
int compress(int *x1, int *x2, int w){
	vector<int> xs(N * 4 + 2);
	xs.push_back(0);
	xs.push_back(w);
	for(int i = 0; i < N; i++){
		for(int j = 0; j <= 1; j++){
			int tx1 = x1[i] - j, tx2 = x2[i] + j;
			if(tx1 >= 0 && tx1 <= w) xs.push_back(tx1);
			if(tx2 >= 0 && 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[i] = distance(xs.begin(), lower_bound(xs.begin(), xs.end(), x1[i]));
		x2[i] = distance(xs.begin(), lower_bound(xs.begin(), xs.end(), x2[i]));
	}
	return xs.size();
}

int main(){
	while(~scanf("%d%d", &W, &H) && W){
		scanf("%d", &N);
		for(int i = 0; i < N; i++) scanf("%d%d%d%d", X1 + i, Y1 + i, X2 + i, Y2 + i);
		W = compress(X1, X2, W);
		H = compress(Y1, Y2, H);
		memset(fld, 0, sizeof(fld));
		// 因为 x, y 是坐标值,而 fld 数组是离散的,填充时为了对应原图矩形,右上边界不填充
		--W, --H;
		for(int i = 0; i < N; i++){
			for(int x = X1[i]; x < X2[i]; x++){
				for(int y = Y1[i]; y < Y2[i]; y++){
					fld[x][y] = 1;
				}
			}
		}
		int res = 0;	
		for(int x = 0; x < W; x++){
			for(int y = 0; y < H; y++){
				if(fld[x][y]) continue;
				++res;
				fld[x][y] = 1;
				// Bfs
				queue<P> que;
				que.push(P(x, y));
				while(!que.empty()){
					P p = que.front(); que.pop();
					int x = p.first, y = p.second;
					for(int i = 0; i < 4; i++){
						int nx = x + dx[i], ny = y + dy[i];
						if(nx >= 0 && nx < W && ny >= 0 && ny < H && !fld[nx][ny]){
							fld[nx][ny] = 1;
							que.push(P(nx, ny));
						}
					}
				}
			}
		}
		printf("%d\n", res);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值