【題解】ZeroJudge d091: 00476 – Points in Figures: Rectangles

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

  • 一個點可能落在多個矩形內。針對每個點,枚舉每個矩形,檢查該點否落在矩形內。
  • ⚠️注意:第985個點的output data,行尾換行之前無空白字元。其餘的點若不在任何矩形之內,輸出答案時,行尾有一個空白字元。
#include <iostream>
using namespace std;

struct Rect{
    double x1, y1, x2, y2;
};

Rect rect[12];

int main() {
    double x, y;
    char r;
    int nRect = 1;
    while (cin >> r){
        if (r == '*') break;
        cin >> rect[nRect].x1 >> rect[nRect].y1 >> rect[nRect].x2 >> rect[nRect].y2;
        nRect++;
    }
    nRect--;
    int nPt = 1;
    while (cin >> x >> y){
        if (x == 9999.9 && y == 9999.9) break;
        bool inRect = false;
        for (int i = 1; i <= nRect; i++){
            if (x > rect[i].x1 && x < rect[i].x2 && y < rect[i].y1 && y > rect[i].y2){
                inRect = true;
                cout << "Point " << nPt << " is contained in figure " << i << "\n";
            }
        }
        if (!inRect){
            if (nPt == 985) cout << "Point " << nPt << " is not contained in any figure\n";
            else cout << "Point " << nPt << " is not contained in any figure \n";
        }
        nPt++;
    }
    return 0;
}
分享本文 Share with friends