题意:
求两个矩形在二维平面内组成的封闭区域的个数
思路:
暴力讨论
赛中队友想暴力搞一下就被我否决了(情况太多有这时间还不如去开其他题?)
考虑对矩形离散化到一个 15 ∗ 15 15*15 15∗15的区域内,矩形的边界用特殊的符号表示,然后对这个区域进行 b f s bfs bfs就完事了。
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const ull seed = 131;
const ll MOD = 1e9 + 7;
using namespace std;
struct Node{
ll a, b;
bool operator < (const Node x) const{
return b > x.b;
}
}p[maxn];
vector<int> xx, yy;
int vis[15][15];
struct cp{
int x, y;
};
int to[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
void bfs(int x, int y, int id){
queue<cp> q;
while(!q.empty()) q.pop();
vis[x][y] = id;
cp a, b;
a.x = x, a.y = y;
q.push(a);
while(!q.empty()){
a = q.front();
q.pop();
for(int i = 0; i < 4; i++){
b.x = a.x + to[i][0];
b.y = a.y + to[i][1];
if(b.x < 0 || b.y < 0 || b.x >= 15 || b.y >= 15) continue;
if(vis[b.x][b.y] != 0) continue;
vis[b.x][b.y] = id;
q.push(b);
}
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
int x[5], y[5];
xx.clear(), yy.clear();
scanf("%d%d%d%d", &x[1], &y[1], &x[2], &y[2]);
scanf("%d%d%d%d", &x[3], &y[3], &x[4], &y[4]);
for(int i = 1; i <= 4; i++){
xx.push_back(x[i]);
yy.push_back(y[i]);
}
sort(xx.begin(), xx.end()), sort(yy.begin(), yy.end());
xx.erase(unique(xx.begin(), xx.end()), xx.end());
yy.erase(unique(yy.begin(), yy.end()), yy.end());
for(int i = 1; i <= 4; i++){
x[i] = lower_bound(xx.begin(), xx.end(), x[i]) - xx.begin() + 1;
x[i] += (x[i] - 1);
}
for(int i = 1; i <= 4; i++){
y[i] = lower_bound(yy.begin(), yy.end(), y[i]) - yy.begin() + 1;
y[i] += (y[i] - 1);
}
memset(vis, 0, sizeof(vis));
for(int i = x[1]; i <= x[2]; i++){
vis[i][y[1]] = -1;
vis[i][y[2]] = -1;
}
for(int i = x[3]; i <= x[4]; i++){
vis[i][y[3]] = -1;
vis[i][y[4]] = -1;
}
for(int i = y[1]; i <= y[2]; i++){
vis[x[1]][i] = -1;
vis[x[2]][i] = -1;
}
for(int i = y[3]; i <= y[4]; i++){
vis[x[3]][i] = -1;
vis[x[4]][i] = -1;
}
int ans = 0;
for(int i = 0; i < 15; i++){
for(int j = 0; j < 15; j++){
if(vis[i][j] != -1 && vis[i][j] == 0){
ans++;
bfs(i, j, ans);
}
}
}
printf("%d\n", ans);
}
return 0;
}