题目地址: http://acm.nudt.edu.cn/showproblem?proid=1558
1558 -- Crazy than before |
Time Limit :1000 MS Memory Limit :65536 KB Accepts : 17 Submits : 46 User Accepts : 17 User Submits : 19 |
Description
Consider the combination of rectangles. Please tell the total area of the combination of the three rectangles.
Input
The first line of the input file is the number of test cases T. The following T lines are the description of three rectangles. In each case, the first rectangle is followed by the second rectangle. the second rectangle is followed by the third rectangle.
Each rectangle is represented by four integers: x1, y1, x2, y2. While (x1, y1) is the low left vertex of the rectangle and (x2, y2) is the high right vertex. You can assume 0<x1<x2<=10000, 0<y1<y2<=10000.
Output
The output for each Case is a line below:
Case i: answer
Where i is the number of the case starting from 1. The answer is the total area of the combination of the three rectangles. There is a blank space between the colon and answer.
Sample Input
1 0 0 2 2 1 1 3 3 2 2 4 4
Sample Output
Case 1: 10
思路:通过构造一个类,来解决问题。以下是 AC 源码:
#include <iostream>
using namespace std;
typedef int TYPE;
class Rect //矩形类,包含四个坐标(左下点和右上点),和自身面积函数,求交集函数 { private:
TYPE x1, y1, x2, y2;
public:
Rect(TYPE a, TYPE b, TYPE c, TYPE d) //构造函数,以四个TYPE类型初始化 { x1 = a;
y1 = b;
x2 = c;
y2 = d; }
const Rect operator * (const Rect &r) const //重载*为求交集 { TYPE tmp_x1 = x1 > r.x1 ? x1 : r.x1; //坐标分别是左下坐标中最大的两个和右上坐标中最小的两个
TYPE tmp_y1 = y1 > r.y1 ? y1 : r.y1;
TYPE tmp_x2 = x2 < r.x2 ? x2 : r.x2;
TYPE tmp_y2 = y2 < r.y2 ? y2 : r.y2;
Rect tmp(tmp_x1, tmp_y1, tmp_x2, tmp_y2); //返回一个矩形
return tmp; }
operator TYPE() const { if (x1 > x2 || y1 > y2) //虚矩形,返回0 { return 0; }
return (x2 - x1) * (y2 - y1); } };
int main() { int T;
cin >> T;
int cases = 1;
while (T--) { TYPE a_x1, a_x2, a_y1, a_y2, b_x1, b_x2, b_y1, b_y2, c_x1, c_x2, c_y1, c_y2;
cin >> a_x1 >> a_y1 >> a_x2 >> a_y2 >> b_x1 >> b_y1 >> b_x2 >> b_y2 >> c_x1 >> c_y1 >> c_x2 >> c_y2 ;
Rect r1(a_x1, a_y1, a_x2, a_y2), r2(b_x1, b_y1, b_x2, b_y2), r3(c_x1, c_y1, c_x2, c_y2);
cout << "Case " << (cases++) << ": " ;
cout << r1 + r2 + r3 - r1 * r2 - r2 * r3 - r1 * r3 + r1 * r2 * r3 << endl; }
return (0); }