POJ 1039 Pipe <计算几何>
来源:互联网 发布:美国博士知乎 编辑:程序博客网 时间:2024/05/21 10:58
题目
分析:初次做计算几何的题,看了一下ljr的《算法艺术与信息学竞赛》,这本书第三章就是计算几何初步,可以用来入门吧。书上p.359有这道例题,看一下前面的基础知识,应该就知道如何做了。
代码:
#include <iostream>#include <iomanip>#include <algorithm>#include <cmath>using namespace std;const double precision=1e-7;const double INF=999999.0;struct Point{ double x,y;};Point up[25],down[25];int n;int dlbcmp(double x){ return fabs(x)<precision?0:(x>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);}bool check(Point& a,Point& b,Point& c,Point& d){ return dlbcmp(cross(a,b,c))*dlbcmp(cross(a,b,d))<=0;//这里允许非规范相交}double intersection(Point& a,Point& b,Point& c,Point& d){ int d1,d2; double s1,s2; d1=dlbcmp(s1=cross(a,b,c)); d2=dlbcmp(s2=cross(a,b,d)); if(d1^d2==2){ return (s2*c.x-s1*d.x)/(s2-s1); } else if(!d1) return c.x; else if(!d2) return d.x; return -INF;}void solve(){ double ans=-INF; for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ if(i==j) continue; int k; for(k=0;k<n;++k){ if(!check(up[i],down[j],up[k],down[k])) break; } if(k==n){ cout<<"Through all the pipe."<<endl; return ; } if(k>max(i,j)){ ans=max(intersection(up[i],down[j],up[k],up[k-1]),ans); ans=max(intersection(up[i],down[j],down[k],down[k-1]),ans); } } } cout<<fixed<<setprecision(2)<<ans<<endl;}int main(){ ios::sync_with_stdio(false); while(cin>>n,n){ for(int i=0;i<n;++i){ cin>>up[i].x>>up[i].y; down[i].x=up[i].x; down[i].y=up[i].y-1; } solve(); } return 0;}
阅读全文
0 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【计算几何+直线相交】
- Pipe - POJ 1039 几何
- POJ 1039 Pipe (计算几何、思维、线段相交)
- POJ 1039-Pipe(计算几何-线段相交、求交点)
- pku 1039 Pipe(计算几何)
- poj1039 Pipe (计算几何)
- poj1039 pipe 计算几何
- hdoj 1454&&poj 1039 Pipe 1454 (数学计算几何) 枚举
- poj1039--Pipe(计算几何)
- poj 1039(计算几何)
- JOJ1076 && POJ1039 Pipe 经典计算几何
- 1097. Deduplication on a Linked List (25)
- 第十六日总结
- 呆萌了
- Scrapy--爬取全国地区对应的天气网址
- poj3468:A Simple Problem with Integers(线段树lazy)
- POJ 1039 Pipe <计算几何>
- test_title
- 刷题#R1
- javascript编解码
- 百度之后, 知乎也要实名制了, 你怎么看?
- MFC消息机制
- 苹果发布智能音箱HomePod,并没有对标亚马逊Echo
- Unity Canvas Scaler详解
- 一个命令即可创建wifi热点