• POJ 1066 Treasure Hunt(转化线段相交)


    Treasure Hunt
    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

    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 


    一伙寻宝人探测到了埃及金字塔底层的宝藏,但是宝藏被n堵墙围着,如果要爆破,只能在每堵墙的中点开门。现在问题来了,对于随机给定的n堵墙,算出最少需要的开门数。

    输入数据如下所示,第一行的整数n表示墙数,后面的n行表示墙体两端坐标,以金字塔边缘左下角为(0,0)右上角为(100,100),每行的四个数据分别为(x1,y1,x2,y2)

    最后一行的数据表示宝藏点P的位置。


    求解:

    用给出的所有点对宝藏点P做线段,找出交点最少的那一组,既是最小开门数。

    1、对于任意P,如果和P只间隔一堵墙,那么,P只需要开一扇门

    证明:无需证明显而易见。

    结论:只要证明墙数,就可以证明门数。

     

    2、边缘上的任意一中点P0对P做线段,交点数等于P到Pi间隔的最小墙数

    证明:

      连接P0-P,交点设为N。

      设,存在一条开门路径,连接P0到P,中间穿过了M堵墙。

      可知,P0-P与开门路径之间是封闭的多边形(路径是直线且与P0-P重合不讨论)。

      有定理一:最小完整路径不会两次闯过同一堵墙。(如果有两次闯过,则至少有三个交点ABC,连接A-C,则路径更少,易证)

      有定理二:两条子路径之间有且仅有一堵墙(易证,穿过墙只能抵达对侧,不能抵达同侧)

     

      由定理二可知,墙一定会经过多边形内部,由定理一可知,墙不会再次经过路径,则,墙一定会经过P0-P

      即是M<=N

     

      

      又有公理一:两条线段最多只有一个交点

      经过了P0-P的线段一定和封闭多边形相交。(由于线段端点落在金字塔边缘,易证)

      即N<=M

     

      所以M=N。

      

      

      

    3、任意现存线段端点与P连线,交点数等于 邻近两个中点分别与P连线 的交点数 中的小值

    证明:

      设有两线段L1,L2分别交金字塔外墙为P1,P2,且P1、P2之间再无其他现存线段的交点

      设有一点Pn处于P1,P2之间

      对Pn-P做连线,设有一现存直线Lx,与L1相交,但不与Pn-P相交,则此时满足Pn对P的交点数小于P1。

      此时,Lx与P1-P2外墙的交点,必然落在Pn和P1之间(此点易证),与P1、P2之间无现存线段交点违背。

      可证,Pn-P的交点数一定大于等于P1-P。

      同理可证Pn和P2关系。

     

      当等于的时候,Pn与P1等价。

      

      当大于的时候,推论如下:

      设有一点Px,Px在Pn-P1延长线上,为P1-P0(P0为另一相交点或者金字塔顶点,相交点等于顶点情况另行讨论)线段上的任意某点。

      

      可知,Px和P1之间再无任意交点。

      那么,沿用先前的推论,P1-P交点数大于等于Px-P0交点数。

      当大于的时候,必有一线段与P1-P相交,不与Px-P0相交,此线段不能落在P1-Px段,只能落在P1-Pn,且不为P1本身(相交非重合),与P1-Pn之间无交点违背。

      所以,必然是等于关系(同理可得L1即是Pn-P多出的那一相交线)

      

      相交点等于顶点情况,如果有P点同侧相交线,相交线必然与金字塔边缘有两个交点,无论落点如何,都能多次引用前半部分推论来同理证得。

      证毕。

     结论:

      只需要求线段端点与P的交点数,即可得到最小值。


    /************************************************************
     * Author        : kuangbin
     * Email         : kuangbin2009@126.com
     * Last modified : 2013-07-14 21:59
     * Filename      : POJ1066TreasureHunt.cpp
     * Description   :
     * *********************************************************/
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <string>
    #include <math.h>
    
    using namespace std;
    const double eps = 1e-8;
    int sgn(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);
        }
        //叉积
        double operator ^(const Point &b)const
        {
            return x*b.y - y*b.x;
        }
        //点积
        double operator *(const Point &b)const
        {
            return x*b.x + y*b.y;
        }
        //绕原点旋转角度B(弧度值),后x,y的变化
        void transXY(double B)
        {
            double tx = x,ty = y;
            x = tx*cos(B) - ty*sin(B);
            y = tx*sin(B) + ty*cos(B);
        }
    };
    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);
        }
        //两条直线求交点,
        //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
        //只有第一个值为2时,交点才有意义
        pair<int,Point> operator &(const Line &b)const
        {
            Point res = s;
            if(sgn((s-e)^(b.s-b.e)) == 0)
            {
                if(sgn((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);
        }
    };
    //两点间距离
    double dist(Point a,Point b)
    {
        return sqrt((a-b)*(a-b));
    }
    
    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) &&
            sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 &&
            sgn((l1.s-l2.s)^(l2.e-l1.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
    }
    
    const int MAXN = 110;
    Line line[MAXN];
    
    Point s;
    
    Point p[MAXN];
    
    
    int main()
    {
        int n;
        while(scanf("%d",&n)==1)
        {
            double x1,y1,x2,y2;
            for(int i = 1;i <= n;i++)
            {
                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
                line[i] = Line(Point(x1,y1),Point(x2,y2));
                p[2*i-1] = Point(x1,y1);
                p[2*i] = Point(x2,y2);
            }
            scanf("%lf%lf",&x1,&y1);
            s = Point(x1,y1);
            int ans = 1000000;
            int xxx;
            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
    ",ans);
        }
    
        return 0;
    }
    


  • 相关阅读:
    sql 数据库 初级 个人学习总结(一)
    parentViewController
    关于iOS9之后的loadViewIfNeeded
    判断版本号
    MagicalRecord(简化CoreData操作)
    coreData
    PureLayout(轻量级自动布局)
    MJRefresh(上拉加载下拉刷新)
    MJExtension(JSON到数据模型的自动转换)
    BaceModel
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717936.html
Copyright © 2020-2023  润新知