1. 程式人生 > >POJ 1066 Treasure Hunt [線段相交]【計算幾何】

POJ 1066 Treasure Hunt [線段相交]【計算幾何】

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。

附本題程式碼

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