Treasure Hunt(poj1066线段相交)

题意:在一个正方形中有多条线段,线段的端点都在正方形的边上,有一个点,然后求从边上到该点最少需要穿过几条线段。

思路:和在一群线段里找一条直线,它穿过所有的线段,而这条直线一定过两条线段的端点,所以可以试想该线段一定是正方形四周的一个点,所以遍历四周的线段的点,找到最小的相交次数。


注意:当n == 0 的时候我刚开始直接输出结果了,其实还要再读一个点!!!!wa哭了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
    double x,y;
    Point(double x = 0,double y = 0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x,a.y+b.y) ;}
Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x,a.y-b.y) ;}
Vector operator * (Vector a,double p) { return Vector(a.x*p,a.y*p) ;}
Vector operator / (Vector a,double p) { return Vector(a.x/p,a.y/p) ;}
double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}
double Length(Vector a) { return sqrt(Dot(a,a)) ;}
double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;}
const double eps = 1e-5;
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;
}
bool operator < (Point a,Point b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

bool SegmentIntersection(Point a,Point b,Point c,Point d)
{
    double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c);
    return dcmp(dcmp(d1)*dcmp(d2)) < 0 && dcmp(dcmp(d3)*dcmp(d4)) < 0;
}

struct Line
{
    Point s,e;
    Line(Point s = 0,Point e = 0) :s(s),e(e){}
};

bool OnIntersection(Point a)
{
    Point p[4];
    p[0] = Point(0,0),p[1] = Point(0,100),p[2] = Point(100,100),p[3] = Point(100,0);
    for(int i = 0; i < 4; i++)
    {
        if(a == p[i])
        return false;
    }
    return true;
}
int main()
{
    int n;
    Line L[100];
    while(scanf("%d",&n) != EOF )
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%lf%lf%lf%lf",&L[i].s.x,&L[i].s.y,&L[i].e.x,&L[i].e.y);
        }
        Point M;
        scanf("%lf%lf",&M.x,&M.y);
        int MIN = 99999;
        for(int i = 0; i < n; i++)
        {
            int ssum = 0,esum = 0;
            for(int j = 0; j < n; j++)
            {
                if(OnIntersection(L[i].s) && SegmentIntersection(L[i].s,M,L[j].s,L[j].e) )
                {
                    ssum++;
                }
                if(OnIntersection(L[i].e) && SegmentIntersection(L[i].e,M,L[j].s,L[j].e) )
                {
                    esum++;
                }
            }
            if(ssum < MIN)
            {
                MIN = ssum;
            }
            if(esum < MIN)
            {
                MIN = esum;
            }
        }
         if(n == 0)
        {
            printf("Number of doors = 1\n");
        }
        else
        printf("Number of doors = %d\n",MIN+1);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值