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

来源:互联网 发布:浙江省高等教育网络 编辑:程序博客网 时间:2024/04/29 18:16

题意:问从(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;}


原创粉丝点击