POJ 2007 Scrambled Polygon(点的极角排序)

来源:互联网 发布:办公软件app 编辑:程序博客网 时间:2024/06/01 09:18

POJ 2007 Scrambled Polygon(点的极角排序)

http://poj.org/problem?id=2007

题意: ZOJ 2352

       给你一个凸多边形的n个点,其中第一个点是(0,0).其他点乱序给出,要你按逆时针顺序输出该凸多边形的所有点.同样第一个点也输出(0,0).

分析:

       由于是凸多边形,所以如果以(0,0)点作为起点,其他所有点与(0,0)点构成了一个向量,我们只需要按照向量的叉积排序即可.

       两个向量A,B(它们有相同的起点(0,0) ). 如果A与B的叉积>0,那么B的另一个端点一定在A的左边. 这样排序之后,最右边的点第二个输出,最左边的点最后一个输出. 所有点就是按逆时针顺序输出的了.

AC代码: 用于POJ

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int maxn=100+10;struct Point{    double x,y;    Point(){}    Point(double x,double y):x(x),y(y){}}P[maxn];typedef Point Vector;Vector operator-(Point A,Point B){    return Vector(A.x-B.x,A.y-B.y);}double Cross(Vector A,Vector B){    return A.x*B.y-A.y*B.x;}bool cmp(Point A,Point B){    return Cross(A-P[0],B-P[0])>0;}int main(){    int n=0;    while(scanf("%lf%lf",&P[n].x,&P[n].y)==2)    {        ++n;    }    sort(P+1,P+n,cmp);    for(int i=0;i<n;++i)        printf("(%.0lf,%.0lf)\n",P[i].x,P[i].y);    return 0;}


AC代码: 用于ZOJ

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int maxn=100+10;struct Point{    double x,y;    Point(){}    Point(double x,double y):x(x),y(y){}}P[maxn];typedef Point Vector;Vector operator-(Point A,Point B){    return Vector(A.x-B.x,A.y-B.y);}double Cross(Vector A,Vector B){    return A.x*B.y-A.y*B.x;}bool cmp(Point A,Point B){    return Cross(A-P[0],B-P[0])>0;}int main(){    int T;    scanf("%d",&T);    scanf("%lf%lf",&P[0].x,&P[0].y);    for(int kase=1;kase<=T;kase++)    {        int n=1;        while(scanf("%lf%lf",&P[n].x,&P[n].y)==2)        {            if(P[n].x==0 && P[n].y==0) break;            ++n;        }        sort(P+1,P+n,cmp);        for(int i=0;i<n;++i)            printf("(%.0lf,%.0lf)\n",P[i].x,P[i].y);        if(kase<T)printf("\n");    }    return 0;}





0 0
原创粉丝点击