题目链接:https://vjudge.net/contest/381753#problem/B
解题报告:
题目大意:
两个矩形能把平面分为多少个区域?
解题思路:
列举两个矩形的关系。别漏,列全~
代码:
#include<bits/stdc++.h> using namespace std; bool check(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { return (x1<=x3&&y1<=y3&&x4<=x2&&y4<=y2); } bool check1(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { return (x3>x1&&y3<y1&&x4<x2&&y4>y2); } bool check2(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { return ( (x3==x1&&y3<y1&&x4<x2&&y4>y2) || (x4==x2&&y3<y1&&x3>x1&&y4>y2) || (x3>x1&&y3==y1&&x4<x2&&y4>y2) || (x3>x1&&y3<y1&&x4<x2&&y4==y2) ); } int main() { int T; scanf("%d",&T); while(T--) { int ans; int x1, y1, x2, y2, x3, y3, x4, y4; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); scanf("%d%d%d%d",&x3,&y3,&x4,&y4); if(x1==x3&&x2==x4&&y1==y3&&y2==y4) { //重合 ans = 2; } else if(x2<=x3||x1>=x4||y2<=y3||y1>=y4) { //相离 ans = 3; } else if(check(x1,y1,x2,y2,x3,y3,x4,y4) || check(x3,y3,x4,y4,x1,y1,x2,y2)) { //包含 if((x1==x3&&x2==x4&&y1!=y3&&y2!=y4) || (y1==y3&&y2==y4&&x1!=x3&&x2!=x4)) { //其中一类情况 ans = 4; } else { ans = 3; } } else { if(check1(x1,y1,x2,y2,x3,y3,x4,y4) || check1(x3,y3,x4,y4,x1,y1,x2,y2)) { //相交,别忘了是对称的 ans = 6; } else if(check2(x1,y1,x2,y2,x3,y3,x4,y4) || check2(x3,y3,x4,y4,x1,y1,x2,y2)) { //其中一类情况 ans = 5; } else { ans = 4; } } printf("%d\n",ans); } }