POJ 1066 Treasure Hunt 计算几何 枚举线段与直线相交

题意 :从边上出发   找到最少的路径去取得宝藏    就是开最少了门    计算几何  枚举线段与直线相交

枚举的线段是所有输入的点 包括墙的四个角的点    直线也一样     最后找出最小的相交次数   减一就是要开的最少门

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define Max 35
using namespace std;

typedef struct
{
    double x,y;
}point;

point p[Max*2];
int p_len;

typedef struct
{
    point a,b;
}Line;

Line l[Max + 5];
int l_len;

double Multi(point p1, point p2, point p0)//叉积
{
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

/*直线与线段的相交判断在线段的端点相交也是相交*/
bool Across(point a, point b, point c, point d)
{
    double tmp = Multi(c, b, a) * Multi(d, b, a);
    if (tmp < 0 || fabs(tmp) < eps) return true;
    return false;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    #endif // LOCAL
    int T;
    while(scanf("%d",&T) != EOF)
    {
        p[0].x = 0;p[0].y = 0;
        p[1].x = 0;p[1].y = 100;
        p[2].x = 100;p[2].y = 100;
        p[3].x = 100;p[3].y = 0;
        l[0].a = p[0];l[0].b = p[1];
        l[1].a = p[1];l[1].b = p[2];
        l[2].a = p[2];l[2].b = p[3];
        l[3].a = p[3];l[3].b = p[0];
        l_len = 4;
        p_len = 2 * T + 4;
        for(int i = 4; i < 2*T+4; i+=2)
        {
            scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+1].x,&p[i+1].y);
            l[l_len].a = p[i];l[l_len++].b = p[i+1];
        }
        point s;
        scanf("%lf%lf",&s.x,&s.y);
        //枚举所有点和treasure点组成的线段
        int minn = 1000;
        for(int i = 0; i < p_len; i++)
        {
            int cnt = 0;
            for(int j = 0; j < l_len; j++)//直线与线段的相交次数
            if(Across(l[j].a,l[j].b,p[i],s))cnt++;
            minn = min(minn,cnt);
        }
        printf("Number of doors = %d\n",minn - 1);//因为是以端点枚举多了一条相交的
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值