Treasure Hunt POJ - 1066

Treasure Hunt POJ - 1066

在这里插入图片描述

思路

感觉这个题的思路就很NICE。看似很复杂,但是实现的很简单。

WA代码

忽略了n为0的情况。在这种情况下,代码会得到INF+1的错误答案。(板子上打错了一个x,我有罪

#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;

double const eps = 1e-8;

int sgn(double x) {
    if (fabs(x) < eps) return 0;
    if (x < 0) return -1;
    else return 1;
}

struct Point {
    double x, y;

    Point() {}

    Point(double _x, double _y) : x(_x), y(_y) {}

    Point operator-(const Point &b) const {
        return Point(x - b.x, y - b.y);
    }

    double operator^(const Point &b) const {
        return x * b.y - y * b.x;
    }
};

struct Line {
    Point s, e;

    Line() {}

    Line(Point _s, Point _e) : s(_s), e(_e) {}

};

bool inter(Line l1, Line l2) {
    return
            max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
            max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
            max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
            max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.s.y) &&
            (sgn((l1.s - l2.e) ^ (l2.s - l2.e)) * sgn((l1.e - l2.e) ^ (l2.s - l2.e)) <= 0) &&
            (sgn((l2.s - l1.e) ^ (l1.s - l1.e)) * sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0);
}

int main() {
    int n;
    scanf("%d", &n);
    vector<Line> l(n + 1);
    for (int i = 0; i < n; i++) {
        scanf("%lf%lf%lf%lf", &l[i].s.x, &l[i].s.y, &l[i].e.x, &l[i].e.y);
    }
    Point tar;
    int ans = 0x3f3f3f3f;
    scanf("%lf%lf", &tar.x, &tar.y);
    for (int i = 0; i < n; i++) {
        Line l1 = Line(tar, l[i].s), l2 = Line(tar, l[i].e);
        int tmp1 = 0, tmp2 = 0;
        for (int j = 0; j < n; j++) {
            if (i == j) continue;
            if (inter(l1, l[j])) tmp1 += 1;
            if (inter(l2, l[j])) tmp2 += 1;
        }
        ans = min(ans, min(tmp1, tmp2));
    }
    printf("Number of doors = ");
    printf("%d\n", ans + 1);
    return 0;
}

AC代码

#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;

double const eps = 1e-8;

int sgn(double x) {
    if (fabs(x) < eps) return 0;
    if (x < 0) return -1;
    else return 1;
}

struct Point {
    double x, y;

    Point() {}

    Point(double _x, double _y) : x(_x), y(_y) {}

    Point operator-(const Point &b) const {
        return Point(x - b.x, y - b.y);
    }

    double operator^(const Point &b) const {
        return x * b.y - y * b.x;
    }
};

struct Line {
    Point s, e;

    Line() {}

    Line(Point _s, Point _e) : s(_s), e(_e) {}

};

bool inter(Line l1, Line l2) {
    return
            max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
            max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
            max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
            max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) &&
            (sgn((l1.s - l2.e) ^ (l2.s - l2.e)) * sgn((l1.e - l2.e) ^ (l2.s - l2.e)) <= 0) &&
            (sgn((l2.s - l1.e) ^ (l1.s - l1.e)) * sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int n;
    scanf("%d", &n);
    vector<Line> l(n + 1);
    for (int i = 0; i < n; i++) {
        scanf("%lf%lf%lf%lf", &l[i].s.x, &l[i].s.y, &l[i].e.x, &l[i].e.y);
    }
    Point tar;
    int ans = 0x3f3f3f3f;
    scanf("%lf%lf", &tar.x, &tar.y);
    for (int i = 0; i < n; i++) {
        Line l1 = Line(tar, l[i].s), l2 = Line(tar, l[i].e);
        int tmp1 = 0, tmp2 = 0;
        for (int j = 0; j < n; j++) {
            if (inter(l1, l[j])) tmp1 += 1;
            if (inter(l2, l[j])) tmp2 += 1;
        }
        ans = min(ans, min(tmp1, tmp2));
    }
    printf("Number of doors = ");
    printf("%d\n", (ans == 0x3f3f3f3f) ? 1 : ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值