凸包(叉積

凸包: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
Description
orm

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;
}
相關文章
相關標籤/搜索