【題解】Green Judge g068: C.幽靈特務報到

【題目敘述】http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=g068
【Tag】struct

#include <iostream>  
using namespace std;  
const int maxn = 100005;  
  
struct Square {  
    double x, y, w, l;  
} sq[maxn];  
  
int main() {  
    ios_base::sync_with_stdio(0);  
    cin.tie(0);  
    int T, N;  
    cin >> T;  
    while (T--) {  
        cin >> N;  
        for (int i = 0; i < N; i++) {  
            cin >> sq[i].x >> sq[i].y >> sq[i].w >> sq[i].l;  
        }  
        double x, y, r;  
        cin >> x >> y >> r;  
        int ans = 0;  
        for (int i = 0; i < N; i++) {  
            double x1 = sq[i].x - sq[i].w / 2;  
            double x2 = sq[i].x + sq[i].w / 2;  
            double y1 = sq[i].y - sq[i].l / 2;  
            double y2 = sq[i].y + sq[i].l / 2;  
              
            if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {  
                //圓心在矩形內  
                ans++;  
                continue;  
            }  
              
            if (x >= x1 && x <= x2) {  
                //檢查矩形的四個邊  
                if (y < y1 && y + r >= y1) {  
                    ans++;  
                    continue;  
                }  
                if (y > y2 && y - r <= y2) {  
                    ans++;  
                    continue;  
                }  
            }  
            if (y >= y1 && y <= y2) {  
                if (x < x1 && x + r >= x1) {  
                    ans++;  
                    continue;  
                }  
                if (x > x2 && x - r <= x2) {  
                    ans++;  
                    continue;  
                }  
            }  
              
            //檢查矩形的四個角  
            double a[4][2] = {{x1, y1}, {x1, y2}, {x2, y1}, {x2, y2}};  
            for (int j = 0; j < 4; j++) {  
                if ((x - a[j][0]) * (x - a[j][0]) + (y - a[j][1]) * (y - a[j][1]) <= r * r) {  
                    ans++;  
                    break;  
                }  
            }  
        }  
        cout << ans << "\n";  
    }  
      
    return 0;  
}  
分享本文 Share with friends