POJ 1066 Treasure Hunt(思维+线段相交判断)

POJ 1066 Treasure Hunt(思维+线段相交判断)

思路:枚举每一个端点,与终点形成线段,与其它线段进行比较,如果有相交,说明要多炸一道墙。同时要将四个角落也加进端点中。这道题比较考思维,但是一般都是要枚举端点的,这一点可以列入这些题型的考虑中。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include <cstdio>
using namespace std;
#define ll long long
int n;
struct Point
{
    double x,y;
    Point(double x= 0, double y = 0):x(x),y(y){}
}p[1000];
typedef Point Vector;
const int INF = 0x3f3f;
Vector operator-(Point A,Point 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, 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);}
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;}
double dis(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
const double eps = 1e-6;
int sgn(double x){
    if(fabs(x) < eps)
        return 0;
    if(x < 0)
        return -1;
    return 1;
}
struct Line{
    Point v, p;
    Line(){}
    Line(Point v, Point p):v(v), p(p){}
    Point point(double t){
        return v+(p-v)*t;//·µ»ØµãP = v + (p - v)*t
    }
}line[105000];
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
    double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
    double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
    return (sgn(c1)*sgn(c2) < 0 && sgn(c3)*sgn(c4) < 0);
}


queue<int>q;
queue<int>temp;
int Count(Point a, Point b)
{
    int cnt  = 1;
    for(int i = 1;i<=n;i++)
    {
        if(SegmentProperIntersection(a,b,line[i].v, line[i].p))
            cnt++;
    }
    return cnt;
}

int main()
{


    while(scanf("%d",&n) !=EOF)
    {
        int cnt = 0;
        p[++cnt] = Point(0, 0);
        p[++cnt] = Point(0, 100);
        p[++cnt] = Point(100, 0);
        p[++cnt] = Point(100, 100);

        for(int i = 1;i<=n;i++)
        {
            Point a, b;
            scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);
            line[i] = Line(a,b);
            p[++cnt] = a;
            p[++cnt] = b;

        }

        Point ed;
        scanf("%lf%lf",&ed.x, &ed.y);
        int minn = 0x3f;
        for(int i = 1;i<=cnt;i++)
        {
            minn = min(Count(p[i],ed), minn);
        }
        printf("Number of doors = %d\n",minn);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值