hdu1392Surround the Trees 凸包
来源:互联网 发布:宁波ug编程培训学校 编辑:程序博客网 时间:2024/06/06 18:59
第一次做凸包,这道题要特殊考虑下,n=2时的情况,要除以二才行。
我是从最左边的点出发,每次取斜率最大的点,一直到最右边的点。
然后从最左边的点出发,每次取斜率最低的点,一直到最右边的点。
#include<stdio.h>#include<math.h>#include<algorithm>#include<string.h>using namespace std;const double eps=1e-9;struct node{double x,y;}f[105];bool flag[105];int n;bool cmp(node a,node b){return fabs(a.x-b.x)<eps?(a.y<b.y):(a.x<b.x);}double xielv(int a,int b)//求斜率{return (f[a].y-f[b].y)/(f[a].x-f[b].x);}double dis(int a,int b)//求距离{double xx=f[a].x-f[b].x,yy=f[a].y-f[b].y;return sqrt(xx*xx+yy*yy);}void calans()//计算最短周长{int i,j,now=0,maxi;double ans=0;while(1){double max=-4000000;//斜率最大值maxi=now;for(i=1;i<n;i++){if(flag[i])continue;if(-f[i].x+f[now].x>eps)continue;//在当前点的左边,不考虑if(fabs(f[now].x-f[i].x)<eps){if(f[now].y>f[i].y)//在当前点的正下方,不考虑 continue; maxi=i; break;}else{double p=xielv(now,i);if(p>max) { maxi=i; max=p; }}}flag[maxi]=1;ans+=dis(now,maxi);if(maxi==now){ flag[maxi]=0;//最后右边的点要计算两次,去除标记 break;}now=maxi;}now=0;while(1)//每次取斜率最小的点{double min=4000000;int mini=now;for(i=1;i<n;i++){if(flag[i])continue;if(-f[i].x+f[now].x>eps)continue;//在当前点的左边,不考虑if(fabs(f[i].x-f[now].x)<eps){if(f[i].x+0.1<f[n-1].x)//在当前点的正下方,不考虑 continue; mini=i; break;}double p=xielv(now,i);if(p<min) { mini=i; min=p; }}ans+=dis(now,mini);flag[mini]=1; if(mini==now) break;now=mini;}if(n==2)ans/=2;//n=2时,除以二printf("%.2f\n",ans);}int main(){int i,j;while(scanf("%d",&n)!=-1&&n){memset(flag,0,sizeof(flag));for(i=0;i<n;i++) scanf("%lf%lf",&f[i].x,&f[i].y); sort(f,f+n,cmp); calans();}return 0;}
- hdu1392Surround the Trees 凸包
- 【凸包问题】HDU1392Surround the Trees
- 专题 计算几何学 凸包 hdu1392Surround the Trees
- hdu_1392_Surround the Trees(凸包)
- Surround the Trees&&凸包入门题
- HDU1392--Surround the Trees--凸包
- HDU 1392 Surround the Trees(凸包)
- hdu_1392 Surround the Trees(凸包)
- HDUJ 1392 Surround the Trees 凸包
- HDOJ_1392 Surround The Trees (凸包模版)
- 凸包问题 hdu1392 Surround the Trees
- HDU1392 Surround the Trees 简单凸包
- hdoj1392Surround the Trees【凸包模板题】
- HDOJ 1392 Surround the Trees (凸包)
- ZJU 1453 Surround the Trees 凸包
- hdu-1392 Surround the Trees(凸包)
- HDU 1392 Surround the Trees(凸包)
- 凸包模版 HDU1392 Surround the Trees
- 输入一个整数,将此整数保存到文件中,以记事本打开,显示同样的内容
- Google Code开源项目
- win7 vmware虚拟机 中 mac系统无法上网
- RIP路由协议
- c++的文件读写
- hdu1392Surround the Trees 凸包
- 期末二
- 8086 CPU 寻址方式
- 总结:复合数据对象
- Opengl Depth Value Transformation
- VS2010编译错误:fatal error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x
- 逆向常用断点设置列表
- 三、初学Java多线程:使用Runnable接口创建线程
- 研发漫画之二:救火还需纵火犯