Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

凸包——卷包裹法

Posted by yc5_yc at 2012-07-19 15:14:45 on Problem 1113
选择一个最靠边缘(按x,y排序)的点,每次用叉积算出相对于上一个点最靠右的点,如果没有可选的点或者最靠右的点被选过了,退出
贴代码:
#define GM pair<double,double>
using namespace std;
const int NMax=1100;
const double PI=3.14159265358979;
int N;
double L;
GM A[NMax];
bool F[NMax];
queue<int> Q;
bool cmp(GM a,GM b) {
    return a.second<b.second || (a.second==b.second && a.first<b.first);
}
double cross(GM c,GM a,GM b) {
    return (c.first-a.first)*(a.second-b.second)-(c.second-a.second)*(a.first-b.first);
}
int main()
{
    scanf("%d%lf",&N,&L);
    for(int i=1;i<=N;i++)
        scanf("%lf%lf",&(A[i].first),&(A[i].second));
    sort(A+1,A+N+1,cmp);
    F[1]=1;
    int last=1;
    while(1)
    {
        int Minn=-1;
        for(int i=1;i<=N;i++) if(!F[i]) {
            Minn=i;
            break;
        }
        if(Minn==-1) break;
        for(int i=1;i<=N;i++) if(cross(A[last],A[i],A[Minn])>0) 
            Minn=i;
        if(F[Minn]) break;
        Q.push(Minn),F[Minn]=1,last=Minn;
    }
    double ans=PI*2*L;
    last=1;
    while(!Q.empty())
    {
        int tmp=Q.front();
        Q.pop();
        ans+=sqrt((A[last].first-A[tmp].first)*(A[last].first-A[tmp].first)
                   +(A[last].second-A[tmp].second)*(A[last].second-A[tmp].second));
        last=tmp;
    }
    ans+=sqrt((A[last].first-A[1].first)*(A[last].first-A[1].first)
                +(A[last].second-A[1].second)*(A[last].second-A[1].second));
    cout.setf(ios::fixed);
    cout.precision(0);
    cout<<ans<<endl;
    getchar();getchar();
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator