[原创]POJ 1066 Treasure Hunt [线段相交]【计算几何】

2016-04-10 18:36:31 Tabris_ 阅读数:549


博客爬取于2020-06-14 22:44:35
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/51114254


题目链接 :http://poj.org/problem?id=1066


Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6260 Accepted: 2598
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


题目大意 :
有一个100*100大的封闭,有着一堆墙,墙的两端都在封闭空间的边上,在封闭空间内有一出宝藏,问你至少要打穿多少墙才能取得宝藏。

解题思路 :
在外围进入里面打多少墙就是计算从这堆墙的两端与宝藏处所连成的线段总共穿过了几道墙(其实想一想也能明白,两点之间直线最短,所以在这样的条件下穿过的墙数最少,就是最少的了),也就是判断一下线段相交,因为数据量并不是很大,所以暴力的枚举就好。
这里应用到了判断线段相交的方法
如果对此不是怎么了解的话可以看下这里:
华丽丽的传送阵:http://blog.csdn.net/qq_33184171/article/details/51114511

提示:
1.计算结果要加1,因为从封闭空间也是用墙封上的。
2.0的时候要特判下,这时结果为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
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstring>
# include <algorithm>
# include <cstdlib>
# include <iomanip>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-8;
int n;

struct point
{
double x,y;
} p;
struct line
{
point p1,p2;
} line1[32],temp;

double multi(point p0,point p1,point p2) //
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

bool IsIntersected(point s1,point e1,point s2,point e2)//两个线段相交
{
return(max(s1.x,e1.x)>=min(s2.x,e2.x))&&
(max(s2.x,e2.x)>=min(s1.x,e1.x))&&
(max(s1.y,e1.y)>=min(s2.y,e2.y))&&
(max(s2.y,e2.y)>=min(s1.y,e1.y))&&
(multi(s1,s2,e1)*multi(s1,e1,e2)>0)&&
(multi(s2,s1,e2)*multi(s2,e2,e1)>0);
}

int sumdoor(line l)
{
int sum=0;
for(int i=0; i<n; i++)
{
if(IsIntersected(line1[i].p1,line1[i].p2,l.p1,l.p2))
sum++;
}
return sum;
}

int main()
{
while(~scanf("%d",&n))
{
double x,y;
int sum=0x1f1f1f1f;
for(int i=0; i<n; i++)
scanf("%lf%lf%lf%lf",&line1[i].p1.x,&line1[i].p1.y,&line1[i].p2.x,&line1[i].p2.y);
scanf("%lf%lf",&p.x,&p.y);
if(n==0) {cout<<"Number of doors = "<<1<<endl;continue;}
temp.p2=p;
for(int i=0; i<n; i++)
{
temp.p1=line1[i].p1;
sum=min(sum,sumdoor(temp));
temp.p1=line1[i].p2;
sum=min(sum,sumdoor(temp));
}

printf("Number of doors = %d\n",sum+1);
}
return 0;
}