凸包:node
大意: 给你若干个点,要你求出能围住全部点最小的点个数,生活中例子:种n颗树,要你围个篱笆 求最小长度。ios
PS:还有要求求面积的web
具体实现算法:算法
1 giftwrapping算法(又叫卷包裹算法,复杂度O(n*h))app
大意:卷包裹算法从一个必然在凸包上的点x开始向着一个方向依次选择最外侧的点n,当回到最初的点时,所选出的点集就是所要求的凸包。ide
x:取一个最左边的也就是横坐标最小的点(或最下边的点,纵坐标最小的),若是有多个这样的点, 就取这些点里纵坐标(横坐标)最小的,这样能够很好的处理共线的状况。 ui
n: 向量的叉积是不知足交换律的,向量A乘以向量B, 若是为正则为A逆时针旋转向B,不然为顺时针,固然这里A转向B的角老是考虑一个小于180度之内的角。spa
例题:code
A - Wall
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit Status
Descriptionorm
Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall.
Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements.
The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.
Input
The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle.
Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.
Output
Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.
Sample Input
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200Sample Output
1628Hint
结果四舍五入就能够了
题意:大体就是要你求将全部点包起来的那个面的最小周长, 以及还有一个以L(第一行第二个数据)为半径圆的周长。。
code:
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<stdio.h> using namespace std; struct node { double x,y; } a[1005],b[1005]; bool cmp(node a, node b) { if(a.y == b.y) return a.x < b.x; return a.y < b.y; } double Cross(node a,node b,node c) { return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } double dis(node a,node b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int zd(node* a,int n,node* b) { sort(a,a+n,cmp); int m=0,i; for(i=0; i<n; i++) { while(m > 1 && Cross(b[m-2],b[m-1],a[i]) < 0) m--; b[m++]=a[i]; } int k=m; for(i=n-2; i>=0; i--) { while(m > k && Cross(b[m-2],b[m-1],a[i]) < 0) m--; b[m++]=a[i]; } if(n >1) m--; return m; } int main() { int n,m; while(cin>>n>>m) { if(n==0) break; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int i,j; for(i=0; i<n; i++) { cin>>a[i].x>>a[i].y; } int k=zd(a,n,b); double s=0; for(i=1; i<k; i++) s+=dis(b[i-1],b[i]); s+=dis(b[0],b[k-1])+3.1415927*m*2; printf("%.0lf\n",s); } return 0; }
2 graham算法:
大意:点排序使用极角排序方式,并对共线状况作特殊处理。通常算法是将共线的点去掉距离小的,保留最远的,这样处理会致使不能输出凸包边上的点,只能输出顶点。可是有时候须要输出这些边上的点,所以最好将共线点都保留,并按照顺序排列。共线点排列方式是:非起始边按照从远道近排列,起始边按从近到远排列。
例题:仍然是上一题
code:
#include <iostream> #include <algorithm> #include <cmath> using namespace std; struct point { double x,y; } p[50010],q[50010]; double multi(point p1,point p2,point p0) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } double dis(point p1,point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } bool cmp(point a,point b)//极角排序 { if (multi(a,b,p[1])>0 || (multi(a,b,p[1])==0&&dis(a,p[1])<dis(b,p[1]))) return true; return false; } int main() { int n,i,j,top; double r; while (cin>>n>>r) { for (i=1; i<=n; i++) { cin>>p[i].x>>p[i].y; if (p[i].y<p[1].y||(p[i].y==p[1].y && p[i].x<p[1].x)) swap(p[1],p[i]); } sort(p+2,p+n+1,cmp); top=0; q[++top]=p[1]; q[++top]=p[2]; for (i=3; i<=n; i++) { while (top>1 && multi(q[top],p[i],q[top-1])<0) top--; q[++top]=p[i]; } double sum=dis(q[1],q[2]); for (i=3; i<=top; i++) sum+=dis(q[i],q[i-1]); sum+=dis(q[1],q[top]); printf("%d",(int)(sum+0.5+3.1415926*r*2)); } return 0; }