POJ 1113 凸包 graham
题目:http://poj.org/problem?id=1113
大意:求出凸包的周长,再加上一个圆的周长就是本问题的答案。
这个问题没有任何的难度,直接采用现成的模板就可以了。

#include "iostream" #include "cstdio" #include "cmath" #include "algorithm" using namespace std; const double PI=acos(-1.0); const int size=1005; const double eps=1e-8; struct Point { int x, y; }p[size]; int s[size], top; int zfcmp(int d) { if(abs(d) < eps) return 0; return d>0?1:-1; } bool cmp(Point p1, Point p2) { if(p1.y==p2.y) return p1.x<p2.x; //这里一定是p1.x<p.x, 不能是p1.x-p2.x else return p1.y<p2.y; } int cross(Point c, Point a, Point b) //c是s[top-1], a是p[i], b是s[top] { return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y); } double dis(Point a, Point b) { return sqrt(0.0+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } void graham_scam(int n) { int i, t; sort(p, p+n, cmp); top = -1; s[++top] = 0; s[++top] = 1; for(i=2; i<n; i++) { while(top>=1 && zfcmp( cross(p[s[top-1]], p[i], p[s[top]]) )>=0 ) top--; s[++top] = i; } t = top; s[++top] = n-2; for(i=n-3; i>=0; i--) { while(top>=t+1 && zfcmp( cross(p[s[top-1]], p[i], p[s[top]]) )>=0 ) top--; s[++top] = i; } } int main() { int i, n, l; double ans; while(cin>>n>>l) { for(i=0; i<n; i++) cin>>p[i].x>>p[i].y; graham_scam(n); ans = 0.0; for(i=0; i<top; i++) ans+=dis(p[s[i]], p[s[i+1]]); ans+=2*PI*l; printf("%0.f\n",ans); } return 0; }
posted on 2011-11-15 21:51 More study needed. 阅读(233) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架