Cod sursa(job #2949802)

Utilizator DooMeDCristian Alexutan DooMeD Data 1 decembrie 2022 19:01:24
Problema Combinari Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.19 kb
#include <bits/stdc++.h>
using namespace std;
using ld = long double;

struct Point {
    int x, y;
    Point operator-(const Point &other) {
        return {x - other.x, y - other.y};
    }
    int operator*(const Point &other) {
        return x * other.x + y * other.y;
    }
};

struct Line {
    int a, b, c;
    Line(Point A, Point B) {
        a = A.y - B.y;
        b = B.x - A.x;
        c = A.x * B.y - A.y * B.x;
    }
};

ld point_to_point(Point a, Point b) {
    return sqrt( (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) );
}

ld point_to_line(Point p, Line d) {
    return abs(d.a * p.x + d.b * p.y + d.c) / sqrt(d.a*d.a + d.b*d.b);
}

bool ok(Point p, Point q, Point r) {
    ld dist;
    if((r-p)*(q-p) >= 0 and (r-q)*(p-q) >= 0) dist = point_to_line(r, Line(p, q));
    else if((r-p)*(q-p) < 0) dist = point_to_point(r, p);
    else dist = point_to_point(r, q);
    return (dist >= 2);
}

int main() {
    ifstream f("snooker.in");
    ofstream g("snooker.out");

    Point start, temp;
    int n, m; f >> n >> m;
    f >> start.x >> start.y >> temp.x >> temp.y;
    vector<Point> targets = {temp};

    int k; f >> k;
    vector<Point> red(k);
    for(int i=0; i<k; i++) f >> red[i].x >> red[i].y;

    // 5 mante => 5 reflexii in fiecare directie
    for(int i=-5; i<=5; i++)
        for(int j=-5; j<=5; j++) {
            if(i == 0 and j == 0) continue;

            for(int ind=0; ind<k; ind++) {
                temp = red[ind];
                if(abs(i) % 2 == 1) temp.x = n - temp.x;
                if(abs(j) % 2 == 1) temp.y = n - temp.y;
                temp.x += i * n;
                temp.y += j * m;
                red.emplace_back(temp);
            }

            temp = targets[0];
            if(abs(i) % 2 == 1) temp.x = n - temp.x;
            if(abs(j) % 2 == 1) temp.y = n - temp.y;
            temp.x += i * n;
            temp.y += j * m;
            targets.emplace_back(temp);
        }

    for(auto B : targets) {
        bool possible = true;
        for(auto C : red) 
            possible = possible & ok(start, B, C);
        if(possible) {
            g << fixed << setprecision(8) << atan2(B.y - start.y, B.x - start.x) << "\n";
            return 0;
        }
    }
    return 0;
}