【POJ-1066】解题报告(计算几何,线段交点)

原始题目

Treasure Hunt

  • Time Limit: 1000MS
  • Memory Limit: 10000K
  • Total Submissions: 7927
  • Accepted: 3269

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

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

Number of doors = 2 

Source

East Central North America 1999

题目大意

在一个100*100的正方形区域中,有n条两端点均在正方形边界的墙将区域分成许多个部分(房间),已知可以破坏每个区域房间墙壁的中点,区域中某个点是宝藏,问从外界到宝藏处最少需要破坏多少个墙壁。

解题思路

  • 由于破坏墙中点后就可以移动到左右端点,故实际上破坏位置在不在中点没有区别。
  • 不可能绕开墙壁,所以走直线最近,每个墙壁最多只会遭遇一次。
  • 所以等价从边界上每个墙壁的端点到宝藏位置连一条线段,和其他线段的交点(严格相交)+1就是所求答案。
  • 利用计算几何中的判断线段是否相交的函数。

解题代码

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//计算几何模板,二维几何基础
//使用印用注意避免直接在程序中调用构造函数构造无名对象。否则可能会导致程序出错
//#include <bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
using namespace std;
typedef long long ll;
const double eps =1e-10;
const int maxn=50;
int n;
//有的命名为sgn函数,高精度符号判断
int dcmp(double x){
//相等函数判断,减少精度问题
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}

//点的定义
class Point{
public:
double x,y;
Point (double x=0,double y=0):x(x),y(y){} //构造函数,方便代码的编写
}point[maxn];

typedef Point Vector;// 从程序实现上,Vector只是Point的别名

//运算符重载
Vector operator + (const Vector &A,const Vector &B) { return Vector(A.x+B.x,A.y+B.y); } //向量+向量=向量,点+向量=点
Vector operator - (const Vector &A,const Vector &B) { return Vector(A.x-B.x,A.y-B.y); } //向量-向量=向量,点-向量-点
Vector operator * (const Vector &A,double p) { return Vector(A.x*p,A.y*p); } //向量*数=向量 (数乘)
Vector operator / (const Vector &A,double p) { return Vector(A.x/p,A.y/p); } //向量/数=向量 (数除)
double operator * (const Vector &A,const Vector &B) { return A.x*B.x+A.y*B.y; } //向量(点乘)向量=数 (点乘)
bool operator < (const Point &A,const Point &B) { return A.x==B.x?A.y<B.y:A.x<B.x; } //按x值递增排序
bool operator == (const Point &A,const Point &B) { return dcmp(A.x-B.x)==0&& dcmp(A.y-B.y)==0; } //判定两个点是否相同,用到dcmp精度判定

//点乘叉乘
double dot(const Vector &A,const Vector &B){ return A.x*B.x+A.y*B.y; } //向量(叉乘)向量=向量 (叉乘)
double operator ^ (const Vector &A,const Vector &B){ return A.x*B.y-A.y*B.x; }
double cross(const Vector &A,const Vector &B){ return A.x*B.y-A.y*B.x; }

//模长面积
double abs(const Vector &A){ return sqrt(dot(A,A));} //计算向量模长
double area2(const Point &A,const Point &B,const Point &C){ return cross(B-A,C-A) ;} //计算平行四边形方向面积
double PolygonArea(Point *p,int n) { double area=0; rep(i,1,n-1){area+=cross(p[i]-p[0],p[i+1]-p[0]);}return area/2.0; } //计算多边形的有向面积

//旋转
Vector rotate(Vector A,double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));} //旋转rad弧度
Vector normal(Vector A){double l=abs(A);return Vector(-A.y/l,A.x/l);} //计算单位法线,左转90

//线段定义
class Line {
public:
Point s,e;
Line(){}
Line(Point _s,Point _e){
s=_s;e=_e;
}

}line[maxn];
//判断两线段是否相交
bool inter(Line l1,Line l2){
// cout<<"L1 "<<l1.e.x<<","<<l1.e.y<<" "<<l1.s.x<<","<<l1.s.y<<endl;
// cout<<"L2 "<<l2.e.x<<","<<l2.e.y<<" "<<l2.s.x<<","<<l2.s.y<<endl;
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) &&
//跨立实验
dcmp((l2.s-l1.s)^(l1.s-l1.e))*dcmp((l2.e-l1.s)^(l1.s-l1.e))<0 &&
dcmp((l1.s-l2.s)^(l2.s-l2.e))*dcmp((l1.e-l2.s)^(l2.s-l2.e))<0
) ;
}
bool inter(Point a1,Point a2,Point b1,Point b2){
Line l1(a1,a2),l2(b1,b2);
return inter(l1,l2);
}
bool cmp(Point a,Point b){
if(a.x==b.x) return a.y<b.y;
else return a.x<b.x;
}

//求两直线交点
Point getinter(Line l1,Line l2){Vector v=l1.s-l1.e;Vector w=l2.s-l2.e;Vector u=l1.e-l2.e;double t=cross(w,u)/cross(v,w);return l1.e+v*t;}
Point getinter(Point a1,Point a2,Point b1,Point b2){Line l1(a1,a2);Line l2(b1,b2);return getinter(l1,l2);}


//判定点和线段的关系,
//0:不在线段所在直线上
//1:在线段内(不含端点)
//2:在线段端点
//3:在线段两侧的射线上
int online(Point a,Line l){
if(dcmp(cross(l.s-a,l.e-a))!=0) return 0;
double pans=dcmp(dot(l.s-a,l.e-a));
// cout<<(l.s-a).x<<","<<(l.s-a).y<<" "<<(l.e-a).x<<","<<(l.e-a).y<<endl;
if(pans<0) return 1;
else if(pans==0) return 2;
else if(pans>0) return 3;
}
int online(Point a,Point b1,Point b2){
Line l(b1,b2);
return online(a,l);
}

//凸包
int ConvexHull(Point *p,int n,Point *ch)
{
sort(p,p+n,cmp);
int m=0;
for(int i=0;i<n;i++){
while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--){
while(m>k && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
if(n>1) m--;
return m;
}



int main(){
cin>>n;
rep(i,0,n){
cin>>line[i].e.x>>line[i].e.y>>line[i].s.x>>line[i].s.y;
}
cin>>point[0].x>>point[0].y;

if(n==0) cout<<"Number of doors = 1"<<endl;
else{
int ans=INF;
rep(i,0,n){
int cnt=0;
Point pn=line[i].e;
Line ln(point[0],pn);
rep(j,0,n){
if(inter(ln,line[j])) cnt++;
}
// cout<<"p1 "<<cnt<<endl;
ans=min(ans,cnt);
cnt=0;
pn=line[i].s;
ln.s=point[0];ln.e=pn;
rep(j,0,n){
if(inter(ln,line[j])) cnt++;
}
ans=min(ans,cnt);
// cout<<"p2 "<<cnt<<endl;
}

cout<<"Number of doors = "<<ans+1<<endl;
}

}

收获与反思

  • 第一次接触计算几何,模板积累。