POJ 1566 The Doors(计算几何+dp)

题意:问从(0,5)点  到 (10,5)点  的最短路;

思路:他一层一层地穿过门,不会返回的,所以可以用dp

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

using namespace std;
const double EPS = 1e-6;
const double INF = 1e20;
struct cvector{
    double x,y;
    cvector(double a,double b){x=a,y=b;}
    cvector(){}
};
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;
}
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);
}
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;
} way[29][2];
double re[29][4];
bool intersect(cline a,cline b)
{
    return ((b.b-a.a)^(a.a-b.a))*((b.b-b.a)^(a.b-b.a))<EPS;
}
double dp[29][4];
bool ok(int f,int t,cline l)
{
    for(int i=f;i<t;i++)
    if(!intersect(way[i][0],l)&&!intersect(way[i][1],l))
    return false;
    return true;
}
int main()
{
    freopen("in.txt","r",stdin);
    int n;
    cpoint st = cpoint(0,5);
    cpoint en = cpoint(10,5);
    double y1,y2,y3,y4,x;
    while(~scanf("%d",&n)&&n>=0)
    {
        if(n==0)
        {
            printf("10.00\n");
            continue;
        }
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
            re[i][0]=y1,re[i][1]=y2,re[i][2]=y3,re[i][3]=y4;
            way[i][0].a=cpoint(x,y1);
            way[i][0].b=cpoint(x,y2);
            way[i][1].a=cpoint(x,y3);
            way[i][1].b=cpoint(x,y4);
        }
        for(int i=0;i<n;i++)
        {
            for(int k=0;k<4;k++)
            {
                dp[i][k] = INF;
                cline tmp;tmp.a = st;tmp.b.x = way[i][0].a.x;tmp.b.y=re[i][k];
                if(ok(0,i,tmp))
                {
                    dp[i][k] = dist(tmp.a,tmp.b);
                    continue;
                }
                for(int j=0;j<i;j++)
                {
                    for(int l=0;l<4;l++)
                    {
                        tmp.a.x=way[j][0].a.x,tmp.a.y=re[j][l];
                        if(ok(j+1,i,tmp))
                        dp[i][k] = min(dp[i][k],dist(tmp.a,tmp.b)+dp[j][l]);
                    }
                }
            }
        }
        cline tmp;tmp.b=en;tmp.a=st;
        if(ok(0,n,tmp))
        {
            printf("10.00\n");
            continue;
        }
        double ans = INF;
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<4;k++)
            {
                tmp.a.x=way[j][0].a.x;
                tmp.a.y=re[j][k];
                if(ok(j+1,n,tmp))
                {
                    ans = min(ans,dist(tmp.a,tmp.b)+dp[j][k]);
                }
            }
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值