凸包求周长

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstdio>
 5 using namespace std;
 6 int n;
 7 typedef struct
 8 {
 9     double x;
10     double y;
11 }Point;
12
13 Point p[110],s[110];
14
15 int top;
16
17 double Mul(Point a,Point b,Point c)
18 {
19     return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
20 }
21
22 double dis(Point a,Point b)
23 {
24     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
25 }
26
27 int cmp(const void *a,const void *b)
28 {
29     Point c=*(Point *)a;
30     Point d=*(Point *)b;
31     double k=Mul(p[0],c,d);
32     if(k<0||(!k&&dis(c,p[0])>dis(d,p[0]))) return 1;
33     return -1;
34 }
35
36 void con()
37 {
38     int i;
39     for(i=1;i<n;i++)
40     {
41         Point temp;
42         if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
43         {
44             temp=p[i];
45             p[i]=p[0];
46             p[0]=temp;
47         }
48     }
49     qsort(p+1,n-1,sizeof(p[0]),cmp);
50     s[0]=p[0];
51     s[1]=p[1];
52     s[2]=p[2];
53     top=2;
54     for(i=3;i<n;i++)
55     {
56         while(top>=2&&Mul(s[top-1],s[top],p[i])<=0) top--;
57         top++;
58         s[top]=p[i];
59     }
60 }
61
62 int main()
63 {
64     while(cin>>n,n)
65     {
66         int i;
67         for(i=0;i<n;i++)
68         {
69             cin>>p[i].x>>p[i].y;
70         }
71         if(n==1)
72         {
73             cout<<"0.00"<<endl;
74             continue;
75         }
76         else if(n==2)
77         {
78             printf("%.2f\n",dis(p[0],p[1]));
79             continue;
80         }
81         con();
82         double coun=0;
83         for(i=0;i<top;i++)
84         {
85             coun+=dis(s[i],s[i+1]);
86         }
87         coun+=dis(s[top],s[0]);
88         printf("%.2f\n",coun);
89     }
90     return 0;
91 }
时间: 2024-10-14 22:54:13

凸包求周长的相关文章

zoj 1453 Surround the Trees(凸包求周长)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453 Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 r

hdu 1348 (凸包求周长)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3229    Accepted Submission(s): 919 Problem Description Once upon a time there was a greedy

(hdu 7.1.7)Wall(求凸包的周长——求将所有点围起来的最小凸多边形的周长)

题目: Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119 Accepted Submission(s): 47   Problem Description Once upon a time there was a greedy King who ordered his chief Architect to build a wa

(hdu step 7.1.7)Wall(求凸包的周长——求将全部点围起来的最小凸多边形的周长)

题目: Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119 Accepted Submission(s): 47   Problem Description Once upon a time there was a greedy King who ordered his chief Architect to build a wa

HDU 4667 Building Fence(求凸包的周长)

A - Building Fence Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Description Long long ago, there is a famous farmer named John. He owns a big farm and many cows. There are two kinds of cows on his farm, o

uva 10065 (凸包+求面积)

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1006 Problem A Useless Tile Packers Input: standard input Output: standard output Yes, as you have apprehended the Useless Tile Pac

POJ 1177 Picture(扫描线求周长)

与求面积并的差不多,但是这个与扫描的方向相同的情况不太好处理,如果扫描线离散化两次扫两遍其实也可以解决这个问题,但是这样无论在时间还是空间上稍微就有点浪费了啊.这里因为我是离散x坐标的所以对于平行于y轴的方向上的统计比较难统计.处理的方法是:标记区间左边的断点,和右边的断点,求出这个区间一共有多少个断点.就可以统计出平行于y轴的长度了.这里合并的时候需要判断右边的左区间和左边的右区间是否相同,如果相同的话,说明他们连接到了一起,要减去多加的. Picture Time Limit: 2000MS

编写一个Shape类,具有属性:周长和面积; 定义其子类三角形和矩形,分别具有求周长的方法。 定义主类E,在其main方法中创建三角形和矩形类的对象, 并赋给Shape类的对象a、b,使用对象a、b来测试其特性。

package shape; public class Shape { //定义成员变量 private double zhouchang; private double mianji; public double getZhouchang() { return zhouchang; } public void setZhouchang(double zhouchang) { this.zhouchang = zhouchang; } public double getMianji() { re

定义一个长方形类,定义 求周长和面积的方法实例

/* 定义一个长方形类,定义 求周长和面积的方法, 然后定义一个测试了Test2,进行测试. 长方形的类: 成员变量: 长,宽 成员方法: 求周长:(长+宽)*2; 求面积:长*宽 注意: import必须出现在所有的class前面.*/ import java.util.Scanner; class ChangFangXing { //长方形的长 private int length; //长方形的宽 private int width; public ChangFangXing(){} //