hdu_1392_Surround the Trees(凸包)
2016-05-26 09:58
435 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1392
题意:求凸包,不知道的百度
题解:模版题
题意:求凸包,不知道的百度
题解:模版题
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; /* * 求凸包,Graham算法 * 点的编号0~n-1 * 返回凸包结果Stack[0~top-1]为凸包的编号 */ const int MAXN = 110; const double eps = 1e-8; const double PI = acos(-1.0); int sgn(double x) { if(fabs(x) < eps)return 0; if(x < 0)return -1; else return 1; } struct Point { double x,y; Point(){} Point(double _x,double _y){x = _x,y = _y;} Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}//叉积 double operator ^(const Point &b)const{return x*b.y-y*b.x;}//点积 double operator *(const Point &b)const{return x*b.x + y*b.y;}//绕原点旋转角度B(弧度值),后x,y的变化 void transXY(double B){double tx = x,ty = y,x = tx*cos(B) - ty*sin(B),y = tx*sin(B) + ty*cos(B);} }list[MAXN]; int S[MAXN],top;//相对于list[0]的极角排序 double dist(Point a,Point b){return sqrt((a-b)*(a-b));} bool _cmp(Point p1,Point p2){ double tmp =(p1-list[0])^(p2-list[0]); if(sgn(tmp)>0)return 1; else if(sgn(tmp)==0&&sgn(dist(p1,list[0])-dist(p2,list[0]))<= 0)return 1; return 0; } void Graham(int n){ Point p0=list[0],tp; int k=0; for(int i=1;i<n;i++)if((p0.y > list[i].y)||(p0.y ==list[i].y&&p0.x>list[i].x))p0 =list[i],k=i; tp=list[k],list[k]=list[0],list[0]=tp,sort(list+1,list+n,_cmp); if(n==1){top=1,S[0]=0;return;} if(n==2){top=2,S[0]=0,S[1]=1;return;} S[0]=0,S[1]=1,top=2; for(int i=2;i<n;i++){ while(top>1&&sgn((list[S[top-1]]-list[S[top-2]])^(list[i]-list[S[top-2]]))<=0)top--; S[top++]=i; } } int main(){ int n; while(~scanf("%d",&n),n){ for(int i=0;i<n;i++)scanf("%lf%lf",&list[i].x,&list[i].y); if(n==1){puts("0.00");continue;} else if(n==2){printf("%.2lf\n",dist(list[0],list[1]));continue;} Graham(n); double ans=0; for(int i=0;i<top-1;i++)ans+=dist(list[S[i]],list[S[i+1]]); ans+=dist(list[S[0]],list[S[top-1]]); printf("%.2lf\n",ans); } return 0; }
相关文章推荐
- 一生受用的格言
- 背水一战 Windows 10 (14) - 动画: 线性动画, 关键帧动画
- View
- oracle中以dba_、user_、v$_、all_、session_、index_开头
- Linux下通过mount挂载iscsi、ntfs、nfs详解
- 实现tableView多选功能及出现问题解决
- update,fixedupdate,lateupdate的区别,解决跟随相机抖动
- TCP是如何保证包的顺序传输?
- JSON基础
- hdu5698 Lucas 大组合数
- java基本数据类型
- PHP封装的MSSql操作类完整实例
- MongoDB基础之九 replication复制集
- 通过GitHub Pages建立个人站点(详细步骤)
- 梦断代码阅读笔记02
- swift基础笔记7-继承
- iOS开发者如何写自己的用户行为统计(仿友盟统计)
- 20个很有用的CSS技巧
- oracle存储过程插入百万数据
- ng-disabled 不起作用的解决办法