NOJ[1511] 围圈

  • 问题描述
  • 在花园里有N颗树,我现在想拿一根绳子将其全部包围。求最小的绳子长度。

  • 输入
  • 先输入一个N(1<=N<=100)表示树的数量。
    接下来N行,每行有两个正整数x和y(int范围内)。
    N=0时表示输入结束。
  • 输出
  • 输出最小需要的绳子长度,保留两位小数。
  • 样例输入
  • 9
    12 7
    24 9
    30 5
    41 9
    80 7
    50 87
    22 9
    45 1
    50 7
    0
    
  • 样例输出
  • 243.06
    
  • 提示
  • 凸包
  • 来源
  • 三胖
凸包的入门题,用的是graham算法。

///凸包,graham—scan

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>

using namespace std;

int stack[110];

struct point
{
int x,y;
}list[110];

int cross(point p0,point p1,point p2)//叉积的计算
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

double dist(point p1,point p2)
{
return sqrt(double((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)));
}

int cmp(point a,point b)
{
int cur=cross(lsit[0],a,b);
if(cur>0)
return 1;
else if(cur==0 && dist(a,list[0]) <dist(b,list[0]))
return 1;
else
return 0;
}

void init(int n)
{
int i,k;
point p0;
scanf("%d%d",&list[0].x,&list[0].y);
p0.x=list[0].x;
p0.y=list[0].y;
k=0;
for(i=1;i<n;i++)
{
scanf("%d%d",&list[i].x,&list[i].y);
if((list[i].y<p0.y) || (list[i].y==p0.y)&&(list[i].x<p0.x))
{
p0.x=list[i].x;
p0.y=list[i].y;
k=i;
}
}
list[k]=list[0];
list[0]=p0;
sort(list+1,list+n,cmp);
}

int graham(int n)
{
int i,top;
if(n==1)
{
top=0;
stack[top]=0;
}
else if(n==2)
{
top=1;
stack[0]=0;
stack[1]=1;
}
else
{
stack[0]=0;
stack[1]=1;
top=1;
for(i=2;i<n;i++)
{
while(top>0 && cross(list[stack[top-1]],list[stack[top]],list[i])<=0 )
top--;
top++;
stack[top]=i;
}
}
return top;
}

int main()
{
int n;
while(~scanf("%d",&n) && n)
{
init(n);
if(n==1)
printf("0.00\n");
else
{
int top=graham(n);
double len=dist(list[0],list[stack[top]]);
while(top)
{
len+=dist(list[stack[top]],list[stack[top-1]]);
top--;
}
if(n>2)
printf("%.2f\n",len);
else
printf("%.2f\n",len/2);
}
}
return 0;
}


andrew算法



#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>

using namespace std;

struct point
{
double x,y;
}list[10010];

int stack[10010];

double x,y;

double cross(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

double dist(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}

int cmp(point a,point b)
{
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
int andrew(int n)
{
sort(list,list+n,cmp);
int m=1;
stack[0]=0;
stack[1]=1;
for(int i=2;i<n;i++)//第一次扫描求下凸包
{
while(m>0 && cross(list[stack[m-1]],list[stack[m]],list[i]) <=0 )
m--;
stack[++m]=i;
}
int k=m;
stack[++m]=n-2;
for(int i=n-3;i>=0;i--)//第二次扫描求上凸包
{
while(m>k && cross(list[stack[m-1]],list[stack[m]],list[i])<=0)
m--;
stack[++m]=i;
}
return m;
}

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);
}
int top=andrew(n);
double len=dist(list[0],list[stack[top]]);
while(top)
{
len+=dist(list[stack[top]],list[stack[top-1]]);
top--;
}
if(n>2)
printf("%.2f\n",len);
else if(n==1)
printf("0.00\n");
else
printf("%.2f\n",len/2);
}
return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值