1. 程式人生 > >CodeForces ~ 994C ~ Two Squares (set + 模擬)

CodeForces ~ 994C ~ Two Squares (set + 模擬)

題意

給你兩個正方形,問兩個正方形是否相交(有一個點相交就算)?第二個正方形為45°擺放,順時針或逆時針輸入這兩個正方形的四個頂點的座標。

思路

因為一個正方形內最多有100*100個點,我們把兩個正方形的點都放入到set中,然後比較,如果有一個相同那麼證明有交點。第一個正方形好處理直接X從min~max,Y從min~max即可。第二個我們從某個對角線往兩邊模擬就好了。

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int X1, Y1, X2, Y2, x, y;
set<pair<int
, int>
> a, b; int main() { X1 = Y1 = INF; X2 = Y2 = -INF; for (int i = 0; i < 4; i++) { scanf("%d%d", &x, &y); X1 = min(X1, x); X2 = max(X2, x); Y1 = min(Y1, y); Y2 = max(Y2, y); } for (int i = X1; i <= X2; i++) { for (int j = Y1; j <= Y2; j++) { a.insert(make_pair(i, j)); } } X1 = Y1 = INF; X2 = Y2 = -INF; for
(int i = 0; i < 4; i++) { scanf("%d%d", &x, &y); X1 = min(X1, x); X2 = max(X2, x); Y1 = min(Y1, y); Y2 = max(Y2, y); } int mid = (X1+X2)/2;//對角線的x座標 for (int i = 0; i <= X2-mid; i++) { for (int j = Y1; j <= Y2; j++) { b.insert(make_pair(mid-i, j)); b.insert(make_pair(mid+i, j)); } Y1++; Y2--; } bool
flag = false; for (auto i: a) { for (auto j: b) { if (i == j) { flag = true; break; } } if (flag) break; } if (flag) printf("YES\n"); else printf("NO\n"); return 0; } /* 0 0 6 0 6 6 0 6 1 3 3 5 5 3 3 1 */