【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees
2014-11-27 17:51
459 查看
【科普】什么是BestCoder?如何参加? |
Surround the TreesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8113 Accepted Submission(s): 3095 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him? The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line. There are no more than 100 trees. Input The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank. Zero at line for number of trees terminates the input for your program. Output The minimal length of the rope. The precision should be 10^-2. Sample Input 9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0 Sample Output 243.06 Source Asia 1997, Shanghai (Mainland China) Recommend Ignatius.L | We have carefully selected several similar problems for you: 2150 1348 1147 1558 1374 |
Jarvis步进法:
算法流程1.找横坐标最小的点(有一样的话纵坐标更小的)
2.从这点开始卷包裹 找最靠近外侧的点(通过叉积比较)
(注意一开始设k为0 若k=0随意找个点为其值)
3.找到的点为起点退出
注:为什么第二步的时候不怕找到过延长线的点?因为不可能存在这个点,若有这个点 在上一个点就被选择拓展了
叉积为0时 注意用点积判断 选择最外面那个点
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <ctime> #include <algorithm> #include <iostream> #include <sstream> #include <string> #define oo 0x13131313 #define exp 10e-6 using namespace std; struct point{ double x,y; }; double ans; int start; point a[101]; int visit[101]; int N; double crossdet(double x1,double y1,double x2,double y2) { return x1*y2-x2*y1; } double cross(point A,point B,point C) { return crossdet(B.x-A.x,B.y-A.y,C.x-A.x,C.y-A.y); //AB*AC } double dotdet(double x1,double y1,double x2,double y2) { return x1*x2+y1*y2; } double dot(point A,point B,point C) { return dotdet(B.x-A.x,B.y-A.y,C.x-A.x,C.y-A.y); //AB dot AC } double dist(point A,point B) { return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); } int sgn(double A) { if(fabs(A)<exp) return 0; else if(A>0) return 1; else return -1; } void input() { ans=0;int sx=40000;int sy=40000; memset(a,0,sizeof(a)); memset(visit,0,sizeof(visit)); for(int i=1;i<=N;i++) { scanf("%lf%lf",&a[i].x,&a[i].y); if(sgn(a[i].x-sx)==-1) { sx=a[i].x;sy=a[i].y; start=i; } else if(sgn(a[i].x-sx)==0&&sgn(a[i].y-sy)==-1) { sx=a[i].x;sy=a[i].y; start=i; } } } void init() { freopen("a.in","r",stdin); freopen("a.out","w",stdout); } int FIND(int now) { int k=0; for(int i=1;i<=N;i++) { if(visit[i]==0&&i!=now) { if(k==0) { k=i; } else { int temp=cross(a[now],a[k],a[i]); if(sgn(temp)<0) k=i; if(sgn(temp)==0) { int temp2=dot(a[k],a[now],a[i]); if(sgn(temp2)<0) k=i; } } } } visit[k]=1; return k; } void solve() { int now=start,next; while(1) { next=FIND(now); ans+=dist(a[now],a[next]); now=next; if(next==start) break; } } int main() { // init(); while(cin>>N&&N) { input(); solve(); if(N==2) { ans=dist(a[1],a[2]); } if(N==1) ans=0; printf("%.2lf\n",ans); } return 0; }
相关文章推荐
- 【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees
- POJ1113(Wall) && Hdu1392(Surround the Trees) (计算几何--凸包)
- HDU 1392 Surround the Trees(计算几何,求凸包周长)
- ACM学习历程—HDU1392 Surround the Trees(计算几何)
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
- HDU1392:Surround the Trees(凸包问题)
- 【凸包】HDU1392 Surround the Trees
- hdu1392 Surround the Trees (求凸包周长)
- 凸包问题 hdu1392 Surround the Trees
- Surround the Trees---hdu1392(凸包GraHam模板)
- HDU1392 Surround the Trees(凸包模版题)
- 凸包模版 HDU1392 Surround the Trees
- ZJU1453 Surround the Trees - 计算几何 经典凸包
- hdu1392(Surround the Trees)凸包问题
- Surround the Trees ssl 1720 计算几何
- HDOJ题目1392Surround the Trees(数学几何,凸包模板)
- HDU1392:Surround the Trees(凸包问题)
- (hdu step 7.1.4)Surround the Trees(求凸包的周长——计算将所有点都包围起来的最小凸多边形的周长)
- hdu_1392 Surround the Trees(凸包)
- HDU 1392 Surround the Trees【凸包周长】