凸包模版和博客

来源:互联网 发布:维多利亚大学 知乎 编辑:程序博客网 时间:2024/06/05 09:15
http://blog.csdn.net/nyist_tc_lyq/article/details/74776760
http://blog.csdn.net/bone_ace/article/details/46239187
http://www.cnblogs.com/Booble/archive/2011/02/28/1967179.html
#include<bits/stdc++.h>using namespace std;struct Z{    double x,y;} z[1000],p[1000];//距离double dis(Z a,Z b){    double d=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);    return d;}//叉积double mm(Z a,Z b,Z c){    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);}//级角排序int cmp(Z a,Z b){    double x=mm(a,b,z[0]);    if(x>0||(x==0&&dis(a,z[0])<dis(b,z[0])))        return 1;    return 0;}//输出排序int cmpp(Z a,Z b){    if(a.x!=b.x)        return a.x<b.x;    else return a.y<b.y;}int main(){    int  t;    cin>>t;    while(t--)    {        int m;        cin>>m;        int w=m;        while(w--)        {            cin>>z[w].x>>z[w].y;        }//Graham扫描法        int k=0;        for(int i=0; i<m; i++)        {            if(z[i].y<z[k].y||(z[i].y==z[k].y&&z[i].x<z[k].x))k=i;        }        swap(z[0],z[k]);        sort(z+1,z+m,cmp);        int top=2;        p[0]=z[0];        p[1]=z[1];        for(int i=2; i<m; i++)        {            while(top>1&&mm(p[top-1],p[top-2],z[i])>=0)            {                top--;            }            p[top++]=z[i];        }//输出要求        sort(p,p+top,cmpp);        cout<<top<<endl;        for(int i=0; i<top; i++)        {            cout<<p[i].x<<" "<<p[i].y<<endl;;        }    }}


原创粉丝点击