1071. Floors

#include <iostream>
#include <vector>
#include <set>

using namespace std;

struct tile{
	int x1, y1, x2, y2;
};

int maxArea;
vector<tile> tiles;
set<int> xs;
set<int> ys;

void init() {
	maxArea = 0;
	tiles.clear();
	xs.clear();
	ys.clear();
}

bool canCut(int x1, int y1, int x2, int y2) {
	for(int i = 0; i < tiles.size(); i++) {
		if(x1 == x2) {
			if(tiles[i].x1 < x1 && tiles[i].x2 > x1 && tiles[i].y1 >= y1 && tiles[i].y2 <= y2) {
				return false;
			}
		}
		else if(y1 == y2) {
			if(tiles[i].y1 < y1 && tiles[i].y2 > y1 && tiles[i].x1 >= x1 && tiles[i].x2 <= x2) {
				return false;
			}
		}
	}
	return true;
}

void cut(int x1, int y1, int x2, int y2) {
	int thisArea = (y2 - y1) * (x2 - x1);
	if(thisArea <= maxArea) {
		return;
	}
	bool cutOnce = false;
	set<int>::iterator it = xs.begin();
	while(it != xs.end() && (*it) <= x1) {
		it++;
	}
	while(it != xs.end() && (*it) < x2) {
		if(canCut((*it), y1, (*it), y2)) {
			cutOnce = true;
			cut(x1, y1, (*it), y2);
			cut((*it), y1, x2, y2);
			break;
		}
		it++;
	}
	if(cutOnce) {
		return;
	}
	it = ys.begin();
	while(it != ys.end() && (*it) <= y1) {
		it++;
	}
	while(it != ys.end() && (*it) < y2) {
		if(canCut(x1, (*it), x2, (*it))) {
			cutOnce = true;
			cut(x1, y1, x2, (*it));
			cut(x1, (*it), x2, y2);
			break;
		}
		it++;
	}
	if(!cutOnce) {
		maxArea = (thisArea > maxArea) ? thisArea : maxArea;
	}
}

int main() {
	int cases;
	cin >> cases;
	while(cases--) {
		init();
		int width, height;
		cin >> width >> height;
		int n;
		cin >> n;
		tiles.resize(n);
		for(int i = 0; i < n; i++) {
			cin >> tiles[i].x1 >> tiles[i].y1
			    >> tiles[i].x2 >> tiles[i].y2;
			xs.insert(tiles[i].x1);
			xs.insert(tiles[i].x2);
			ys.insert(tiles[i].y1);
			ys.insert(tiles[i].y2);
		}
		cut(0, 0, width, height);
		cout << maxArea << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值