POJ 1066 线段相交

题目的意思换句话说:就是要你以线段中的某个点和财宝(treasure)所在点连成一条直线,使得其和其它线段交点数最少;

这样理解的条件是: 财宝点 不在墙上.题目已给出,满足;

另外注意的是,初始化最小值肯定是一个很大的值inf; 如果最后得到的是inf,那就置为0(最后为1); 可能很多人错在这儿

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 30+3;
#define eps 1e-8

struct Point
{
    float x;
    float y;
}p[maxn*2],P0;

int Sig(float x)
{
    return (x > eps) - (x < -eps);
}

float Mult(Point p0, Point p1, Point p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y);
}

int line_in_line(Point p1, Point p2, Point p3, Point p4)
{
    return ( Sig(Mult(p1,p3,p2))*Sig(Mult(p1,p4,p2)) < 0 && Sig(Mult(p3,p1,p4))*Sig(Mult(p3,p2,p4)) < 0 );
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
#endif
    int n;
    cin>>n;
    for(int i = 0; i < n*2; i += 2)
    {
        cin>>p[i].x>>p[i].y>>p[i+1].x>>p[i+1].y;
    }
    cin>>P0.x>>P0.y;
    int MIN = INF, num = 0;
    for(int i = 0; i < n*2; i++)
    {
        num = 0;
        for(int j = 0; j < n*2-1; j += 2)
        {
            if(line_in_line(P0,p[i],p[j],p[j+1]))
                num++;
        }
//        cout<<i<<" "<<num<<endl;
        MIN = num < MIN ? num: MIN;
    }
    if(MIN == INF )
        MIN = 0;
    cout<<"Number of doors = "<<MIN+1<<endl;

    return 0;
}


你可能感兴趣的:(POJ 1066 线段相交)