POJ 1392(凸包的周长)
2015-07-18 11:17
302 查看
求凸包的长度,先求出凸包的外围的点(求法上篇说过)。
然后求每每两个点的之间的距离就可以了。
如果求凸包的面积就可以使用。
公式是:
s1=x1*y2-y1*x2;
S=s1+s2+...s(n-1)+s(n)(共有n个点)
s/=2;
然后求每每两个点的之间的距离就可以了。
如果求凸包的面积就可以使用。
公式是:
s1=x1*y2-y1*x2;
S=s1+s2+...s(n-1)+s(n)(共有n个点)
s/=2;
#include<stdio.h> #include<algorithm> #include<math.h> using namespace std; const int maxn=100000; int n,k,e; struct node { double x,y; } st[maxn],num[maxn]; double dis(node p1,node p2) { return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y); } double multi(node p1,node p2,node p3) { return (p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y); } bool cmp(node p1,node p2) { if(multi(p1,p2,num[0])>0)return true; if(multi(p1,p2,num[0])==0&&dis(p1,num[0])<dis(p2,num[0]))return true; return false; } void Graham() { e=2; st[0]=num[0]; st[1]=num[1]; st[2]=num[2]; for(int i=3; i<n; i++) { while(e>1&&multi(num[i],st[e],st[e-1])>=0) e--; st[++e]=num[i]; } e++; st[e]=num[0]; double sum=0; for(int i=0; i<e; i++) sum+=sqrt(dis(st[i],st[i+1])); printf("%.2lf\n",sum); } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; k=0; for(int i=0; i<n; i++) { scanf("%lf%lf",&num[i].x,&num[i].y); if(num[i].x<num[k].x||(num[i].x==num[k].x&&num[i].y<num[k].y))k=i; } if(n==1) { puts("0.00"); continue; } if(n==2) { double s=0; s=sqrt(dis(num[0],num[1])); printf("%.2lf\n",s); continue; } swap(num[0],num[k]); sort(num+1,num+n,cmp); Graham(); } return 0; } /* 8 0 0 1 0 2 0 2 1 2 2 1 2 0 2 0 1 */
相关文章推荐
- windows启动服务出现 193 0xc1错误的解决方法
- Java数据库连接(JDBC)之一:JDBC详细连接过程
- 安全机制-验证码实例
- Compiler Error Message: CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework64
- 微信公众平台开发前言
- 南阳 oj 动态规划 回文字符串
- Android Studio AIDL 的使用 。
- nohup后台运行以及重定向标准输出和标准异常
- 《C++ Primer》 ---- 关于变量 与 基本类型
- Delete Node in a Linked List
- C语言中如何将二维数组作为函数的参数传递
- 重新思考上路
- 三层架构的思考
- Skype for Business Server 2015-13-IISARR-2-发布-3-OWAS
- Android--ViewDragHelper完全解析 自定义ViewGroup神器
- 素数环
- hdu 1558 Segment set (并查集)
- 课程设计-银行储蓄系统
- freemarker中循环显示
- 排序算法之简单选择排序