POJ 1066 Treasure Hunt

本文介绍了一种解决二维房间寻宝问题的方法,通过枚举外围点来计算到达指定位置所需的最少开门次数。利用线段相交判断算法实现,并提供完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

http://poj.org/problem?id=1066

题意:

在一个100*100的二维房间内加上一些墙,求进入到这个房间某一处(x,y)拿宝藏至少要开几个门,外墙也要开门。

思路:

直接枚举外围的所有点,求到宝藏要穿过的线段数就行了。

代码:

#define N 112

int n,m;
int flag,sum,ave,ans,res;
struct Point{
    double x,y;
};
struct Line{
    Point s,e;
}line[N],pos;
double cross3(Point p0,Point p1,Point p2){
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool isMeet(Line l1,Line l2)
{
    double x=cross3(l1.s,l2.s,l2.e);
    double y=cross3(l1.e,l2.s,l2.e);
    double xx=cross3(l2.s,l1.s,l1.e);
    double yy=cross3(l2.e,l1.s,l1.e);
    if(!x||!y||!xx||!yy)return false;
    if((x<0&&y>0||x>0&&y<0)&&(xx<0&&yy>0||xx>0&&yy<0))return true;
    return false;
}
int check(Line u)
{
    int sum=0;
    for(int i=0;i<n;i++)
        if(isMeet(u,line[i]))
            sum++;
    return sum;
}
int main()
{
    int i,j,k,kk,cas,T,t,x,y,z;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)
            scanf("%lf%lf%lf%lf",&line[i].s.x,&line[i].s.y,&line[i].e.x,&line[i].e.y);
        scanf("%lf%lf",&pos.s.x,&pos.s.y);
        res=INF;
        for(i=0;i<n;i++)
        {
            pos.e=line[i].s;
            res=min(res,t=check(pos));
            pos.e=line[i].e;
            res=min(res,t=check(pos));
        }
        pos.e.x=0;pos.e.y=0;
        res=min(res,check(pos));
        pos.e.x=0;pos.e.y=100;
        res=min(res,check(pos));
        pos.e.x=100;pos.e.y=0;
        res=min(res,check(pos));
        pos.e.x=100;pos.e.y=100;
        res=min(res,check(pos));
        printf("Number of doors = %d\n",res+1);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值