zju1453(二维凸包)

Surround the Trees

Time limit: 1 Seconds   Memory limit: 32768K  
Total Submit: 532   Accepted Submit: 199  

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

题目的主要大意是:给出平面上的若干个点,求其最小外接凸多边形(周长最短)的周长,算法就是先求凸包,然后计算其周长即可,代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;

const int N = 105;
const double PI = 3.1425926;
const double MAX = 10e100;
const double eps = 0.000001;

typedef struct TYPE
{
 double x, y, r;
}Point;
int swap(Point&, Point&);
int cmp(const void*, const void*);
bool check(Point, Point, Point);
double dis(Point, Point);

int main()
{
 int n, i, j;
 double s, t;
 Point a[N], temp;
 while (1)
 {
  scanf("%d", &n);
  if (n == 0)
   break;
  temp.x = temp.y = MAX;
  for (i = 0; i < n; i++)
  {
   scanf("%lf%lf", &(a[i].x), &(a[i].y));
   if ((a[i].x < temp.x) || (a[i].x == temp.x && a[i].y < temp.y))
    temp = a[i], j = i;
  } //找出最左下的点
  if (n == 1)
  {
   printf("0.00/n");
   continue;
  }
  else if (n == 2)
  {
   printf("%.2lf/n", 2*dis(a[0], a[1]));
   continue;
  }
  swap(a[0], a[j]);
  a[0].r = -1;
  for(i = 1; i < n; i++)
  {
   t = (a[0].y - a[i].y) / dis(a[0], a[i]);
   if(fabs(t) < eps) a[i].r = PI/2;
   else if(fabs(t-1) < eps) a[i].r = 0;
   else if(fabs(t+1) < eps) a[i].r = PI;
   else a[i].r = acos(t);
  } //以a[0]为极点, 水平方向对极轴建立极坐标系, 算出其余各点的极角
  qsort(a, n, sizeof(a[0]), cmp); //按极角排序
  for (i = j = 2; i < n; )
  {
   if (check(a[j-2], a[j-1], a[i]))
    a[j++] = a[i], i++;
   else
    j--;
  } //Graham Scan算法求凸包
  for (i = 0, s = 0; i < j - 1; i++)
   s += dis(a[i], a[i+1]);
  s += dis(a[0], a[j-1]);
  printf("%.2lf/n", s);
 }
 return 0;
}

int swap(Point& p, Point& q)
{
 double temp;
 temp = p.x, p.x = q.x, q.x = temp;
 temp = p.y, p.y = q.y, q.y = temp;
 return 0;
}

int cmp(const void *a, const void *b)
{
 double temp = ((Point*)a)->r - ((Point*)b)->r;
 if (temp > 0)
  return 1;
 else if(fabs(temp) < eps)
  return 0;
 else 
  return -1;
}

bool check(Point p, Point q, Point r)
{
 double x1 = q.x - p.x, y1 = q.y - p.y;
 double x2 = r.x - q.x, y2 = r.y - q.y;
 if (x1*y2 - x2*y1 >= 0)
  return true;
 else
  return false;
}

double dis(Point p, Point q)
{
 double x1 = q.x - p.x, y1 = q.y - p.y;
 return sqrt(x1*x1 + y1*y1);
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值