- 樣例輸入
-
5 0 0 4 5 0 0 3 1 0 1 2 5 3 0 4 5 2 2 3 5
- 樣例輸出
-
2 1 3 2
描述
小Hi在玩一個拼圖遊戲。如下圖所示,整個拼圖是由N塊小矩形組成的大矩形。現在小Hi發現其中一塊小矩形不見了。給定大矩形以及N-1個小矩形的頂點座標,你能找出缺失的那塊小矩形的頂點座標嗎?
輸入
第一行包含一個整數,N。
第二行包含四個整數,(X0, Y0), (X'0, Y'0),代表大矩形左下角和右上角的座標。
以下N-1行每行包含四個整數,(Xi, Yi), (X'i, Y'i),代表其中一個小矩形的左下角和右上角座標。
對於30%的數據, 1 <= N <= 1000
對於100%的數據,1 <= N <= 100000 所有的座標(X, Y)滿足 0 <= X, Y <= 100000000
輸出
輸出四個整數(X, Y), (X', Y')代表缺失的那塊小矩形的左下角和右上角的座標。
思路:
把矩形的四個點算出來,統計一下,缺失的矩形的四個頂點出現的次數一定是奇數次
#include <iostream>
#include <set>
#include <map>
#include <vector>
using namespace std;
struct Node {
int x, y;
Node(int a, int b) {
x = a, y = b;
}
bool operator < (const Node &b) const {
if (x < b.x) return true;
else if (x == b.x ) {
if (y < b.y) return true;
else
return false;
}
return false;
}
};
void findAndInsert(set<Node> &S, Node tmp) {
set<Node>::iterator it;
it = S.find(tmp);
if ( it != S.end()) {
S.erase(tmp);
}
else {
S.insert(tmp);
}
}
void solve() {
int n, x1, y1, x2, y2, x3, y3, x4, y4;
cin >> n;
set<Node>S;
for (int i = 0; i < n; ++i) {
cin >> x1 >> y1 >> x2 >> y2;
x3 = x1, y3 = y2;
x4 = x2, y4 = y1;
Node leftDown = Node(x1, y1);
Node rightUp = Node(x2, y2);
Node leftUp = Node(x3, y3);
Node rightDown = Node(x4, y4);
findAndInsert(S, leftUp);
findAndInsert(S, leftDown);
findAndInsert(S, rightUp);
findAndInsert(S, rightDown);
}
set<Node>::iterator it = S.begin();
int cnt = 0;
for (; it != S.end(); ++it) {
if (cnt == 0 || cnt == 3) {
cout << (*it).x << " " << (*it).y << " ";
}
++cnt;
}
cout << endl;
}
int main() {
solve();
return 0;
}