POJ 1066 Treasure Hunt

题意:从给定点到边框外,至少要经过多少根线,不能从交点经过。。

思路:连接给定点与边框上的任意一点,求与这边相交的最小边数。因为给出的线与边框的交点都是整数点,所以只要判断每一个点的左右两边附近的两个点就行了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;
const double EPS = 1e-6;
struct cvector{
    double x,y;
    cvector(double a,double b){x=a,y=b;}
    cvector(){}
};
cvector operator+(cvector a,cvector b){
    return cvector(a.x+b.x,a.y+b.y);
}
cvector operator-(cvector a,cvector b){
    return cvector(a.x-b.x,a.y-b.y);
}
cvector operator*(double a,cvector b){
    return cvector(a*b.x,a*b.y);
}
double operator*(cvector a,cvector b){
    return a.x*b.x+a.y*b.y;
}
double operator^(cvector a,cvector b){
    return a.x*b.y-b.x*a.y;
}
double length(double t){return t>0?t:-t;}
double length(cvector t){return sqrt(t*t);}
struct cpoint{
    double x,y;
    cpoint(double a,double b){x=a,y=b;}
    cpoint(){}
};
cvector operator-(cpoint a,cpoint b)
{
    return cvector(b.x-a.x,b.y-a.y);
}
double dist(cpoint a,cpoint b){
    return length(b-a);
}
struct cline{
    cpoint a,b;
};
bool intersect(cline a,cline b)
{
    return ((a.a-b.a)^(b.b-b.a))*((a.b-b.a)^(b.b-b.a))<EPS;
}
cline re[39];
cpoint p[69];
bool operator <(cpoint a,cpoint b)
{
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
int n;
int oor(cline tmp)
{
    int ans =0;
    for(int i=0;i<n;i++)
    if(intersect(tmp,re[i]))
    ans++;
    return ans;
}
int main()
{
    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lf%lf%lf%lf",&re[i].a.x,&re[i].a.y,&re[i].b.x,&re[i].b.y);
        p[i<<1].x=re[i].a.x,p[i<<1].y=re[i].a.y;
        p[i<<1|1].x=re[i].b.x,p[i<<1|1].y=re[i].b.y;
    }
    cline tmp;
    int ans = 0x3f3f3f3f;
    scanf("%lf%lf",&tmp.a.x,&tmp.a.y);
    if(n==0)
    {
        ans=0;
    }
    for(int i=0;i<(n<<1);i++)
    {
        if(p[i].x==0||p[i].x==100)
        {
            tmp.b.x=p[i].x,tmp.b.y=p[i].y+0.5;
            ans = min(ans,oor(tmp));
            tmp.b.x=p[i].x,tmp.b.y=p[i].y-0.5;
            ans = min(ans,oor(tmp));
        }
        if(p[i].y==0||p[i].y==100)
        {
            tmp.b.x=p[i].x+0.5,tmp.b.y=p[i].y;
            ans = min(ans,oor(tmp));
            tmp.b.x=p[i].x-0.5,tmp.b.y=p[i].y;
            ans = min(ans,oor(tmp));
        }
    }
    printf("Number of doors = %d\n",ans+1);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值