POJ 1066 Treasure Hunt (线段相交)

题目:

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 

题意:在金字塔中有一个宝藏P(x,y) 金字塔中有无数堵墙 只有墙的中心可以被通过 求问要取到宝藏最少要通过多少堵墙
思路:枚举从四面墙进入 枚举每个端点与P点的连线要经过几堵墙 当墙数为零时输出1

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=110;
const double eps=1e-10;
int n;
double x,y,xx,yy,tx,ty;

int dcmp(double x){
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    else return 1;
}

struct Point{
    double x,y;
    Point(){}
    Point(double _x,double _y){
        x=_x,y=_y;
    }
    Point operator + (const Point &b) const{
        return Point(x+b.x,y+b.y);
    }
    Point operator - (const Point &b) const{
        return Point(x-b.x,y-b.y);
    }
    double operator * (const Point &b) const{
        return x*b.x+y*b.y;
    }
    double operator ^ (const Point &b) const{
        return x*b.y-y*b.x;
    }
    void transXY(double B){
        double tx=x,ty=y;
        x=tx*cos(B)-ty*sin(B);
        y=tx*sin(B)+ty*cos(B);
    }
}p[maxn],s;

struct Line{
    Point s,e;
    double k;
    Line(){}
    Line(Point _s,Point _e){
        s=_s,e=_e;
        k=atan2(e.y-s.y,e.x-s.x);
    }
    pair<int,Point> operator & (const Line &b) const{
        Point res=s;
        if(dcmp((s-e)^(b.s-b.e))==0){
            if(dcmp((s-b.e)^(b.s-b.e))==0)
                return make_pair(0,res);
            else return make_pair(1,res);
        }
        double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x+=(e.x-s.x)*t;
        res.y+=(e.y-s.y)*t;
        return make_pair(2,res);
    }
}line[maxn];

double dist(Point a,Point b){
    return sqrt((b-a)*(b-a));
}

bool inter(Line l1,Line l2){
    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.e-l1.s))*dcmp((l2.e-l1.s)^(l1.e-l1.s))<=0 &&
        dcmp((l1.s-l2.s)^(l2.e-l1.s))*dcmp((l1.e-l2.s)^(l2.e-l2.s))<=0;
}

int main(){
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
            line[i]=Line(Point(x,y),Point(xx,yy));
            p[2*i-1]=Point(x,y);
            p[2*i]=Point(xx,yy);
         }
         scanf("%lf%lf",&tx,&ty);
         s=Point(tx,ty);
         int ans=inf;
         for(int i=1;i<=2*n;i++){
             int tmp=0;
             Line line1=Line(s,p[i]);
             for(int j=1;j<=n;j++){
                 if(inter(line1,line[j]))
                     tmp++;
             }
             ans=min(ans,tmp);
         }
         Line line1;
         line1=Line(s,Point(0,0));
         int tmp=0;
         for(int i=1;i<=n;i++){
             if(inter(line1,line[i]))
                 tmp++;
         }
         ans=min(ans,tmp+1);
         line1=Line(s,Point(0,100));
         tmp=0;
         for(int i=1;i<=n;i++){
             if(inter(line1,line[i]))
                 tmp++;
         }
         ans=min(ans,tmp+1);
         line1=Line(s,Point(100,100));
         tmp=0;
         for(int i=1;i<=n;i++){
             if(inter(line1,line[i]))
                 tmp++;
         }
         ans=min(ans,tmp+1);
         line1=Line(s,Point(100,0));
         tmp=0;
         for(int i=1;i<=n;i++){
             if(inter(line1,line[i]))
                 tmp++;
         }
         ans=min(ans,tmp+1);
         printf("Number of doors = %d\n",ans);
    }
    return 0;
}

 

 
posted @ 2018-10-24 13:06  周园顾  阅读(156)  评论(0编辑  收藏  举报