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;
}