/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 1ms 540.0 KiB
#3 Accepted 1ms 540.0 KiB
#4 Accepted 1ms 540.0 KiB
#5 Accepted 1ms 332.0 KiB
#6 Accepted 1ms 540.0 KiB
#7 Accepted 2ms 516.0 KiB
#8 Accepted 1ms 540.0 KiB
#9 Accepted 1ms 540.0 KiB
#10 Accepted 1ms 340.0 KiB
#11 Accepted 2ms 540.0 KiB
#12 Accepted 2ms 512.0 KiB
#13 Accepted 1ms 388.0 KiB
#14 Accepted 1ms 328.0 KiB
#15 Accepted 1ms 772.0 KiB
#16 Accepted 2ms 540.0 KiB
#17 Accepted 2ms 328.0 KiB
#18 Accepted 2ms 772.0 KiB
#19 Accepted 2ms 544.0 KiB
#20 Accepted 2ms 588.0 KiB
#21 Accepted 2ms 552.0 KiB
#22 Accepted 2ms 516.0 KiB
#23 Accepted 2ms 444.0 KiB
#24 Accepted 2ms 324.0 KiB
#25 Accepted 2ms 540.0 KiB
#26 Accepted 2ms 332.0 KiB
#27 Accepted 2ms 344.0 KiB
#28 Accepted 1ms 332.0 KiB
#29 Accepted 1ms 540.0 KiB

Code

#include <iostream>
#include <vector>
using namespace std;

struct Point {
    int x, y;
};

// Function to check if a point is strictly inside a polygon
bool isPointInsidePolygon(const vector<Point>& polygon, Point p) {
    int n = polygon.size();
    bool inside = false;

    for (int i = 0, j = n - 1; i < n; j = i++) {
        // Check if the point lies within the y-bounds of the edge
        if ((polygon[i].y > p.y) != (polygon[j].y > p.y)) {
            // Check if the ray crosses the edge
            double intersectionX = polygon[j].x + (p.y - polygon[j].y) * 1.0 * (polygon[i].x - polygon[j].x) / (polygon[i].y - polygon[j].y);
            if (p.x < intersectionX) {
                inside = !inside;
            }
        }
    }

    return inside;
}

int main() {
    int n;
    cin >> n;

    vector<Point> polygon(n);
    for (int i = 0; i < n; i++) {
        cin >> polygon[i].x >> polygon[i].y;
    }

    Point p;
    cin >> p.x >> p.y;

    if (isPointInsidePolygon(polygon, p)) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1145 Nobita's Love for Shizuka
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-14 19:41:19
Judged At
2024-12-14 19:41:19
Judged By
Score
100
Total Time
2ms
Peak Memory
772.0 KiB