【題解】ZeroJudge e568: 11639 – Guard the Land

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e568
【解題想法】

  • 兩個矩形有重疊部份時,下面兩個條件要同時成立:
    • max(G1.x1, G2.x1) < min(G1.x2, G2.x2)
    • max(G1.y1, G2.y1) < min(G1.y2, G2.y2)
#include <iostream>
using namespace std;

struct guard{
    int x1, x2, y1, y2;
};

int main() {
    int T;
    guard G1, G2;
    cin >> T;
    for (int Case = 1; Case <= T; Case++){
        cin >> G1.x1 >> G1.y1 >> G1.x2 >> G1.y2;
        cin >> G2.x1 >> G2.y1 >> G2.x2 >> G2.y2;
        int strong = 0;
        if (max(G1.x1, G2.x1) < min(G1.x2, G2.x2) &&
            max(G1.y1, G2.y1) < min(G1.y2, G2.y2)){
            strong = (min(G1.x2, G2.x2) - max(G1.x1, G2.x1)) *
            (min(G1.y2, G2.y2) - max(G1.y1, G2.y1));
        }
        int weak = (G1.x2 - G1.x1) * (G1.y2 - G1.y1) + 
                   (G2.x2 - G2.x1) * (G2.y2 - G2.y1) - 2 * strong;
        cout << "Night " << Case << ": ";
        cout << strong << " " << weak << " " 
             << 10000 - strong - weak << "\n";
    }
    return 0;
}
Sample Input:
 4
 44 4 55 24
 66 66 90 69
 26 0 68 12
 10 70 61 83
 13 5 50 71
 23 14 44 27
 25 1 80 84
 13 34 91 90
Sample Output:
 Night 1: 0 292 9708
 Night 2: 0 1167 8833
 Night 3: 273 2169 7558
 Night 4: 2750 3433 3817

分享本文 Share with friends