Problem Description
Calabash is the servant of a landlord. The landlord owns a piece of land, which can be regarded as an infinite 2D plane.
One day the landlord set up two orthogonal rectangular-shaped fences on his land. He asked Calabash a simple problem: how many nonempty connected components is my land divided into by these two fences, both finite and infinite? Calabash couldn’t answer this simple question. Please help him!
Recall that a connected component is a maximal set of points not occupied by the fences, and every two points in the set are reachable without crossing the fence.
Input
The first line of input consists of a single integer T (1≤T≤10000), the number of test cases.
Each test case contains two lines, specifying the two rectangles. Each line contains four integers x1,y1,x2,y2 (0≤x1,y1,x2,y2≤109,x1<x2,y1<y2), where (x1,y1),(x2,y2) are the Cartesian coordinates of two opposite vertices of the rectangular fence. The edges of the rectangles are parallel to the coordinate axes. The edges of the two rectangles may intersect, overlap, or even coincide.
Output
For each test case, print the answer as an integer in one line.
Sample Input
3
0 0 1 1
2 2 3 4
1 0 3 2
0 1 2 3
0 0 1 1
0 0 1 1
Sample Output
3
4
2
给你两个矩形,问这两个矩形可以将平面分割成几个部分。
结果只有2,3,4,5,6着五种结果,其中4的时候情况特别多,所以特殊处理,其余几种情况比较少,所以直接判断就好。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a1,a2,a3,a4;
int b1,b2,b3,b4;
bool check3()
{
if(a1==b1&&a3==b3&&(a2>b2&&a4<b4||b2>a2&&b4<a4)) return 0;
if(a2==b2&&a4==b4&&(b1>a1&&b3<a3||a1>b1&&a3<b3)) return 0;
if(a1<=b1&&a2<=b2&&a3>=b3&&a4>=b4) return 1;
if(b1<=a1&&b2<=a2&&b3>=a3&&b4>=a4) return 1;
if(b1>=a3||b2>=a4||a1>=b3||a2>=b4) return 1;
return 0;
}
bool check5()
{
if(a1>b1&&a3<b3&&a4==b4&&a2<b2) return 1;
if(a1>b1&&a3<b3&&a2==b2&&a4>b4) return 1;
if(a2<b2&&a4>b4&&a3==b3&&b1<a1) return 1;
if(a2<b2&&a4>b4&&a1==b1&&b3>a3) return 1;
if(b1>a1&&b3<a3&&b4==a4&&b2<a2) return 1;
if(b1>a1&&b3<a3&&b2==a2&&b4>a4) return 1;
if(b2<a2&&b4>a4&&b3==a3&&a1<b1) return 1;
if(b2<a2&&b4>a4&&b1==a1&&a3>b3) return 1;
return 0;
}
bool check6()
{
if(a1>b1&&a3<b3&&a2<b2&&b4<a4) return 1;
if(b1>a1&&b3<a3&&b2<a2&&a4<b4) return 1;
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&a1,&a2,&a3,&a4);
scanf("%d%d%d%d",&b1,&b2,&b3,&b4);
if(a1==b1&&a2==b2&&a3==b3&&a4==b4) printf("2\n");
else if(check3()) printf("3\n");
else if(check5()) printf("5\n");
else if(check6()) printf("6\n");
else printf("4\n");
}
return 0;
}
emmmm,这个题也可以利用染色法去做,离散化之后bfs去判断。
#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
int mp[12][12];
int n = 10,m = 10;
void bfs(int x,int y,int num){
queue<int> q;
q.push(x*10+y);
mp[x][y] = num;
while(q.size()){
int nx = q.front()/10;
int ny = q.front()%10;
q.pop();
for(int i = 0;i<4;i++){
int tx = dir[i][0]+nx;
int ty = dir[i][1]+ny;
if(tx>=n || ty>=m || tx<0 || ty<0) continue;
if(mp[tx][ty] == 0){
mp[tx][ty] = num;
q.push(tx*10+ty);
}
}
}
}
int main(){
int t;cin>>t;
while(t--){
int ax1,ax2,bx1,bx2,ay1,ay2,by1,by2;
scanf("%d%d%d%d",&ax1,&ay1,&ax2,&ay2);
scanf("%d%d%d%d",&bx1,&by1,&bx2,&by2);
memset(mp,0,sizeof(mp));
int x[5],y[5];
int lx,ly;
x[1] = ax1;x[2] = ax2;x[3] = bx1;x[4] = bx2;
y[1] = ay1;y[2] = ay2;y[3] = by1;y[4] = by2;
sort(x+1,x+5);
sort(y+1,y+5);
lx = unique(x+1,x+5)-x-1;
ly = unique(y+1,y+5)-y-1;
map<int,int> mpx,mpy;
for(int i = 1;i<=lx;i++){
mpx[x[i]] = i;
}
for(int i = 1;i<=ly;i++){
mpy[y[i]] = i;
}
ax1 = mpx[ax1]*2-1;ax2 = mpx[ax2]*2-1;bx1 = mpx[bx1]*2-1;bx2 = mpx[bx2]*2-1;
ay1 = mpy[ay1]*2-1;ay2 = mpy[ay2]*2-1;by1 = mpy[by1]*2-1;by2 = mpy[by2]*2-1;
for(int i = ax1;i<=ax2;i++){
mp[i][ay1] = mp[i][ay2] = -1;
}
for(int i = ay1;i<=ay2;i++){
mp[ax1][i] = mp[ax2][i] = -1;
}
for(int i = bx1;i<=bx2;i++){
mp[i][by1] = mp[i][by2] = -1;
}
for(int i = by1;i<=by2;i++){
mp[bx1][i] = mp[bx2][i] = -1;
}
int num = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(mp[i][j] == 0){
num++;
bfs(i,j,num);
}
}
}
printf("%d\n",num);
}
return 0;
}
努力加油a啊,(o)/~