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;}
- POJ 1566 The Doors(计算几何+dp)
- POJ 1556 The Doors(Dijkstra+计算几何)
- poj 1556 The Doors(计算几何)
- 【计算几何】 POJ 1556 The Doors
- POJ 1556 The Doors 计算几何+MST
- POJ 1556 The Doors (计算几何+dij最短路)
- POJ 1556 The Doors 计算几何+ 最短路
- POJ 1556 The Doors(计算几何+最短路)
- POJ 1556 The Doors(计算几何+Floyd)
- poj 1556 The Doors(最短路+计算几何)
- poj1556 The Doors 计算几何+最短路
- ZOJ 1721 The Doors dijstra+计算几何
- POJ 1556 The Doors(几何+最短路)
- poj 1556 The Doors (bellman+几何)
- POJ 1556 The Doors --几何,最短路
- poj 1556 The Doors(几何+最短路)
- poj 1556 The Doors(最短路+几何)
- POJ 1556 The Doors (计算几何判断线段相交+最短路)
- HDU1850 Being a Good Boy in Spring Festivaly
- 【解题报告】 NYOJ 78 -- 圈水池 Andrew 凸包算法
- Activity intent经常使用的 FLAG 集合(
- tar命令详解
- Oracle dbms_job package 用法小结
- POJ 1566 The Doors(计算几何+dp)
- Devexpress 绑定树
- eclipse导入Android项目,Override不能正确解析
- Oracle 行列转换 总结
- SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法【已验证
- 投影空间坐标与屏幕的对应关系
- Android Power management
- IOS VIEW截图并保存为PNG的方法
- 在Linux下怎么看网络流量