POJ 1066 Treasure Hunt 判断线段相交(求交点个数)

Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4875 Accepted: 2022

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 

题目大意:

      在一座金字塔的底部,有一条条横垮金字塔底四周的墙。这些墙纵横交错,然而在金字塔的中间某个地方有一个财宝。我们要得到财宝就必须从最外面的四周墙面打门,问我们最少需要打穿多少个洞(门),门必须开在两个端点的中点,才能得到宝藏。

解题思路:

      这一题我刚拿手上也是丈二和尚摸不着头脑,后来看了别人的解题报告都说方法十分巧妙。让我们看看题目所给的图。在38和47中间打了个洞。我们不禁会有疑问,我在38那里打个洞不是一样的?虽然有时会因为角度不同而得出不同的答案,但是我们要是枚举每一个在四周的点,把它和宝藏穿起来构成一条线段,再判断这条线段和多少个墙是有交点的,那么我们就知道从这个端点的旁边开一个洞通到宝藏还需要打多少个洞了。最后的答案肯定就是最少的交点数+1(这个是四周墙面的洞,它不会和其他的墙有交点)。当然这里还要用的中心算法就是judge_range()和judge_cross()了,可以说是判断线段相交的模板算法吧,在我前一篇博文中有更细致的讲述。注意不要忘了枚举四个角,忘了这个我就wa了一发。。。

代码如下:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 32
#define eps 10e-9
int n;
int ans[65];

struct Point 
{
    double x,y;
}treasure;

struct Segment
{
    Point s,e;
}wall[32],s[65];

double cross ( Point a, Point b , Point o)
{
    return ( a.x - o.x ) * ( b.y - o.y )  - ( a.y - o.y ) * ( b.x - o.x );
}

bool judge_cross ( Segment a , Segment b )
{
    double t1 = cross ( a.s , b.s , a.e );
    double t2 = cross ( a.s , b.e , a.e );
    double t3 = cross ( b.s , a.s , b.e );
    double t4 = cross ( b.s , a.e , b.e );
    if ( t1 * t2 < 0.0 - eps && t3 * t4 < 0.0- eps ) return true;
    return false;
}

bool judge_range ( Segment a , Segment b )
{
    if ( min ( a.s.x , a.e.x ) > max ( b.s.x , b.e.x ) ||
          min ( a.s.y , a.e.y ) > max ( b.s.y , b.e.y ) ||
          min ( b.s.x , b.e.x ) > max ( a.s.x , a.e.x ) ||
          min ( b.s.y , b.e.y ) > max ( a.s.y , a.e.y ))
        return false;
    return true;
}

bool check_cross ( Segment a , Segment b )
{
    if ( judge_range ( a , b ) && judge_cross ( a, b ) ) return true;
    return false;
}

int main()
{
    scanf ("%d" , &n );
    if ( n == 0 )
    {
        printf ( "Number of doors = 1\n" );
        return 0;
    }
    for ( int i = 0 ; i < n ; i ++ )
        scanf ( "%lf %lf %lf %lf" , &wall[i].s.x , &wall[i].s.y , &wall[i].e.x ,&wall[i].e.y );
    scanf ( "%lf %lf" , &treasure.x , &treasure.y );
    memset ( ans , 0 , sizeof ( ans ) );
    for ( int i = 0 ; i < n ; i ++ )  //将所有枚举的端点和宝藏点连起来,构成一条线段,总共会有2*n条线段
    {
        s[i].s = wall[i].s;
        s[i].e = treasure;
        s[i+n].s = wall[i].e;
        s[i+n].e = treasure;
    }
    for ( int i = 0 ; i < 2*n ; i ++ )
    {
        for ( int j = 0 ; j < n ; j ++ )
        {
            if ( check_cross ( s[i] , wall[j] ) ) ans[i] ++;
        }
    }
    
    Segment angle[4];  //四个角
    angle[0].s.x = 0;
    angle[0].s.y = 0;
    angle[0].e = treasure;
    
    angle[1].s.x = 0;
    angle[1].s.y = 100;
    angle[1].e = treasure;
    
    angle[2].s.x = 100;
    angle[2].s.y = 0;
    angle[2].e = treasure;
    
    angle[3].s.x = 100;
    angle[3].s.y = 100;
    angle[3].e = treasure;
    
    int ans_angle[4] = { 0 , 0, 0 ,0 };
    
    for ( int i = 0 ; i < 4 ; i ++ )
        for ( int j = 0 ; j < n ; j ++ )
            if ( check_cross ( angle[i] , wall[j] ) ) ans_angle[i] ++;
    int res = 99999999;
    
    for ( int i = 0 ; i < 2 * n ; i ++ )
        if ( ans[i] < res ) res = ans[i];
    for ( int i = 0 ; i < 4 ; i ++ )
        if ( ans_angle[i] < res ) res = ans_angle[i];
    printf ( "Number of doors = %d\n" , res + 1);
    return 0;
}


技巧总结:

      总之计算几何牵涉到线段相交的题目,一定要注意找端点,找直线,找线段,用叉乘判断相交,百变不离其宗。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值