1. 程式人生 > >POJ 1066 - Treasure Hunt - [枚舉+判斷線段相交]

POJ 1066 - Treasure Hunt - [枚舉+判斷線段相交]

created pro 金字塔 from lang int scanf col ber

題目鏈接:http://poj.org/problem?id=1066

Time Limit: 1000MS Memory Limit: 10000K

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

題意:

一個正方形底的金字塔,坐標為(0,0)->(100,100),裏面情況類似於上圖,有許多直接連接在最外層正方形上的墻,把整個金字塔底部分割成許多小房間;

現在專家們已經確定,其中某一個房間為寶藏房,並且得到了一個位於該房間內的點坐標,記為點p;

現在專家們要從最外面進行爆破開門法,一直炸到寶藏房,求最少需要開多少扇門。

題解:

枚舉(0,0)-(0,100)-(100,100)-(100,0)這個正方形上的所有點(其實就是所有墻的端點),記為點q;

連接p與q兩點得到一條線段,再去枚舉所有的墻,通過判斷是否規範相交確定一路上要經過多少堵墻,記為cnt;

順便把(0,0)、(0,100)、(100,100)、(100,0)這四個點也按上面的辦法去算一下cnt;

取所有cnt中最小的,加上1(外墻上還要開扇門),即為答案;

AC代碼:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;

const double eps = 1e-6;

struct Point{
    double x,y;
    Point(double tx=0,double ty=0):x(tx),y(ty){}
};
typedef Point Vctor;

//向量的加減乘除
Vctor operator + (Vctor A,Vctor B){return Vctor(A.x+B.x,A.y+B.y);}
Vctor operator - (Point A,Point B){return Vctor(A.x-B.x,A.y-B.y);}
Vctor operator * (Vctor A,double p){return Vctor(A.x*p,A.y*p);}
Vctor operator / (Vctor A,double p){return Vctor(A.x/p,A.y/p);}

int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return (x<0)?(-1):(1);
}
bool operator == (Point A,Point B){return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;}

double Cross(Vctor A,Vctor B){return A.x*B.y-A.y*B.x;}

//判斷線段是否規範相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    double c1 = Cross(a2 - a1,b1 - a1), c2 = Cross(a2 - a1,b2 - a1),
           c3 = Cross(b2 - b1,a1 - b1), c4 = Cross(b2 - b1,a2 - b1);
    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}

int n;
struct Seg{
    Point a,b;
}wall[33];
Point p;
int test(const Point& q)
{
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(SegmentProperIntersection(p,q,wall[i].a,wall[i].b)) cnt++;
    return cnt;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%lf%lf%lf%lf",&wall[i].a.x,&wall[i].a.y,&wall[i].b.x,&wall[i].b.y);
    cin>>p.x>>p.y;

    int ans=0x3f3f3f3f;
    for(int i=1,tmp;i<=n;i++)
    {
        ans=min(test(wall[i].a),ans);
        ans=min(test(wall[i].b),ans);
    }
    ans=min(test(Point(0,0)),ans);
    ans=min(test(Point(0,100)),ans);
    ans=min(test(Point(100,0)),ans);
    ans=min(test(Point(100,100)),ans);

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

POJ 1066 - Treasure Hunt - [枚舉+判斷線段相交]