POJ 1039 Pipe
来源:互联网 发布:用到云计算的行业 编辑:程序博客网 时间:2024/04/30 19:25
题目大意:有个宽度为1的管子,上面各点为(x1,y1), (x2,y2), ...... ,(xn,yn),相对应的下顶点为(xi,yi-1),i=1,2,3……,n。光线从入口处(x1,y1),(x1,y1-1)间射入,问光线所能达到的最远距离。假设光只沿直线前进,管子不透光。
思路:
(1)按照黑书上的结论,最优光线必然是擦到一个上顶点和一个下顶点。
(2)利用定比分点公式求相交线段的交点,假设线段AB与线段CD规范相交于P,则:
S(△ABD) / S(△ACB) = DP / CP = (Xp - Xd) / (Xc- Xp) => Xp = (S(△ABD) * Xc + S(△ABC) * Xd) / (S(△ABD) + S(△ABC))
(3)依次枚举每个上顶点up[i]和下顶点bottom[j]构成一条直线,判断该直线是否符合条件,即是否能穿过max(i,j)之前的所有管道。求出所有符合条件的最远距离。
#include <iostream>#include <cstdio>#include <cmath>#include <cfloat>#include <algorithm>using namespace std;const double precision=1e-6;typedef struct{ double x,y;}Point;int dbl_cmp(double d){ if (fabs(d)<precision) return 0; return d>0?1:-1;}double det(double x1,double y1,double x2,double y2){ return x1*y2-x2*y1;}double cross(Point a,Point b,Point c){ return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);}int segcross_simple(Point a,Point b,Point c,Point d){ if (dbl_cmp(cross(a,b,c))*dbl_cmp(cross(a,b,d))<=0) return 1; return 0;}int segcross(Point a,Point b,Point c,Point d,Point &p){ double s1,s2; int d1,d2; s1=cross(a,b,c); s2=cross(a,b,d); d1=dbl_cmp(s1); d2=dbl_cmp(s2); if ((d1^d2)==-2) { p.x=(c.x*s2-d.x*s1)/(s2-s1); return 1; } if (d2==0) { p.x=d.x; return 1; } if (d1==0) { p.x=c.x; return 1; } return 0;}int main(){ Point up[21],bottom[21],p; int n,i,j,k; bool through; double ans; while (scanf("%d",&n)==1 && n!=0) { through=false; ans=-DBL_MAX; for (i=1;i<=n;i++) { scanf("%lf%lf",&up[i].x,&up[i].y); bottom[i].x=up[i].x; bottom[i].y=up[i].y-1.0; } for (i=1;i<=n && through==false;i++) for (j=1;j<=n;j++) { for (k=1;k<=n;k++) if (segcross_simple(up[i],bottom[j],up[k],bottom[k])==0) break; if (k>n) { through=true; break; } if (k>max(i,j)) { segcross(up[i],bottom[j],up[k],up[k-1],p); ans=max(ans,p.x); segcross(up[i],bottom[j],bottom[k],bottom[k-1],p); ans=max(ans,p.x); } } if (through) printf("Through all the pipe.\n"); else printf("%.2lf\n",ans); } return 0;}
- POJ 1039 Pipe
- POJ 1039 Pipe
- POJ 1039 Pipe
- POJ 1039 Pipe
- POJ 1039 Pipe
- POJ 1039 Pipe
- POJ 1039 Pipe
- POJ 1039 Pipe
- poj 1039 Pipe
- POJ 1039 pipe
- POJ 1039--Pipe
- poj 1039 Pipe
- Pipe - POJ 1039 几何
- POJ 1039 Pipe
- poj 1039 Pipe
- POJ-1039-Pipe
- POJ 1039 Pipe
- poj 1039 Pipe
- 详解java序列化(一)
- cocoa UITableView数据分批显示
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-2)
- .NET WebService的创建、部署、运用
- WebSphere创建删除概要文件
- POJ 1039 Pipe
- kernel启动控制台还不可用时发生crash的调试方法
- 日记...抽象工厂
- Linux经验总结:linux的入门学习流程(菜鸟发贴,仅供参考)(更新RHCE完结)
- 《C和指针》介绍及pdf下载
- 一个带过滤功能的combobox组件
- 数据丢失找专家
- 盘点家里哪些地方最容易滋生细菌
- 一段时间……