POJ 1066 线段相交判断

题目链接http://poj.org/problem?id=1066


题解:线段相交判断

  要通过的“门”最少,也就是该点与边框点的连线也其他线段的交点最少。


#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 35
using namespace std;
//定义点
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector; //Vector 为 Point的别名

//点-点=向量
Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}

struct segment
{
    Point a, b;
}seg[N];

const double eps = 1e-10;
int dcmp(double x)
{
	if(fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}

//叉积:两向量v和w的叉积等于v和w组成的三角形的有向面积的两倍 XaYb - XbYa
double Cross(Vector A, Vector B)
{
	return A.x*B.y - A.y*B.x;
}

//每条线段的两个端点都在另一条线段的两侧
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)//a,b线段
{
	double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1),
		   c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);

	return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4) < 0;
}
int main ()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%lf %lf %lf %lf", &seg[i].a.x, &seg[i].a.y, &seg[i].b.x, &seg[i].b.y);
    }

    Point final;
    scanf("%lf %lf", &final.x, &final.y);
    int ans = 1000;

    for(int i = 1; i <= n; i++)
    {
        int cnt = 0;
        for(int j = 1; j <= n; j++)
            if(SegmentProperIntersection(seg[j].a, seg[j].b, final, seg[i].a))
                cnt++;
        ans = min(cnt, ans);

        cnt = 0;
        for(int j = 1; j <= n; j++)
            if(SegmentProperIntersection(seg[j].a, seg[j].b, final, seg[i].b))
                cnt++;
        ans = min(cnt, ans);
    }


    if(n == 0) printf("Number of doors = 1\n"); 
    else
    printf("Number of doors = %d\n", ans+1);


    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值