0%

POJ 1066 Treasure Hunt (线段相交)

题目链接:POJ 1066

Description

Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-art technology they are able to determine that the lower floor of the pyramid is constructed from a series of straightline walls, which intersect to form numerous enclosed chambers.

Currently, no doors exist to allow access to any chamber. This state-of-the-art technology has also pinpointed the location of the treasure room. What these dedicated (and greedy) archeologists want to do is blast doors through the walls to get to the treasure room. However, to minimize the damage to the artwork in the intervening chambers (and stay under their government grant for dynamite) they want to blast through the minimum number of doors. For structural integrity purposes, doors should only be blasted at the midpoint of the wall of the room being entered. You are to write a program which determines this minimum number of doors.

An example is shown below:

Input

The input will consist of one case. The first line will be an integer n (0 <= n <= 30) specifying number of interior walls, followed by n lines containing integer endpoints of each wall x1 y1 x2 y2 . The 4 enclosing walls of the pyramid have fixed endpoints at (0,0); (0,100); (100,100) and (100,0) and are not included in the list of walls. The interior walls always span from one exterior wall to another exterior wall and are arranged such that no more than two walls intersect at any point. You may assume that no two given walls coincide. After the listing of the interior walls there will be one final line containing the floating point coordinates of the treasure in the treasure room (guaranteed not to lie on a wall).

Output

Print a single line listing the minimum number of doors which need to be created, in the format shown below.

Sample Input

1
2
3
4
5
6
7
8
9
7 
20 0 37 100
40 0 76 100
85 0 0 75
100 90 0 90
0 71 100 61
0 14 100 38
100 47 47 100
54.5 55.4

Sample Output

1
Number of doors = 2

Source

East Central North America 1999

Solution

题意

给定 $n$ 条线段,代表围墙,最外层为 $100 \times 100$ 的正方形围墙。给定宝藏的坐标,求从外面进去拿到宝藏最少要穿过几堵墙。

思路

枚举正方形边上的所有端点与宝藏构成的线段,与多少堵墙相交,相交最少的即为答案。

线段 $A$ 与线段 $B$ 相交的判断:$A$ 的两个端点在线段 $B$ 的两边且线段 $B$ 的两个端点在线段 $A$ 的两边。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef double db;
const db eps = 1e-8;
const int inf = 0x3f3f3f3f;

inline int dcmp(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
}

class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
void input() {
scanf("%lf%lf", &x, &y);
}
bool operator<(const Point &a) const {
return (!dcmp(x - a.x))? dcmp(y - a.y) < 0: x < a.x;
}
bool operator==(const Point &a) const {
return dcmp(x - a.x) == 0 && dcmp(y - a.y) == 0;
}
Point operator+(const Point a) {
return Point(x + a.x, y + a.y);
}
Point operator-(const Point a) {
return Point(x - a.x, y - a.y);
}
Point operator*(double p) {
return Point(x * p, y * p);
}
Point operator/(double p) {
return Point(x / p, y / p);
}
db dot(const Point a) {
return x * a.x + y * a.y;
}
db cross(const Point a) {
return x * a.y - y * a.x;
}
};

class Line {
public:
Point s, e;
db angle;
Line() {}
Line(Point s, Point e) : s(s), e(e) {}
inline void input() {
scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);
}
bool operator<(const Line &a) const {
Line l = a;
if(dcmp(angle - l.angle) == 0) {
return l.toLeftTest(s) == 1;
}
return angle < l.angle;
}
int toLeftTest(Point p) {
if((e - s).cross(p - s) > 0) return 1;
else if((e - s).cross(p - s) < 0) return -1;
return 0;
}
int segcrossseg(Line l) {
if(l.toLeftTest(s) * l.toLeftTest(e) == -1 && toLeftTest(l.s) * toLeftTest(l.e) == -1) {
return 1;
} else {
return 0;
}
}
Point crosspoint(Line l) {
double a1 = (l.e - l.s).cross(s - l.s);
double a2 = (l.e - l.s).cross(e - l.s);
double x = (s.x * a2 - e.x * a1) / (a2 - a1);
double y = (s.y * a2 - e.y * a1) / (a2 - a1);
if(dcmp(x) == 0) x = 0;
if(dcmp(y) == 0) y = 0;
return Point(x, y);
}
};

Line l[100];
Point tar;
int n;

int f(Line l1) {
int sum = 0;
for(int i = 0; i < n; ++i) {
if(l[i].segcrossseg(l1)) {
++sum;
}
}
return sum;
}

int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
l[i].input();
}
tar.input();
int ans = inf;
for(int i = 0; i < n; ++i) {
Line l1 = Line(l[i].s, tar);
Line l2 = Line(l[i].e, tar);
int cnt1 = 0, cnt2 = 0;
for(int j = 0; j < n; ++j) {
if(i == j) continue;
if(l[j].segcrossseg(l1)) {
++cnt1;
}
if(l[j].segcrossseg(l2)) {
++cnt2;
}
}
ans = min(ans, min(cnt1, cnt2));
}
Point tmp = Point(0, 0);
ans = min(ans, f(Line(tmp, tar)));
tmp = Point(0, 100);
ans = min(ans, f(Line(tmp, tar)));
tmp = Point(100, 0);
ans = min(ans, f(Line(tmp, tar)));
tmp = Point(100, 100);
ans = min(ans, f(Line(tmp, tar)));
printf("Number of doors = %d\n", ans + 1);
return 0;
}

欢迎关注我的其它发布渠道