pku 2007 grahamScan扫描法求凸包

ContractedBlock.gif ExpandedBlockStart.gif Code
#include <iostream>
#include 
<algorithm>
using namespace std;

const int MAXN = 55;
typedef 
struct
{
    
double x,y;
}Point;
Point p[MAXN];

Point p0
={0,0};
bool cmp1(const Point &a,const Point &b)//y升序,y相同x升序
{
    
if( a.y == b.y ) return a.x < b.x;
    
else return a.y < b.y;
}

double xmult(Point p1,Point p2,Point p0)//叉积>0右拐
{
    
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

bool cmp2(const Point &a,const Point &b)//极角升序
{
    
if( xmult(a,b,p0) < 0 ) return false;
    
else return true;
}

int main()
{
    
int n = 0,i;
    
//freopen("2007.txt","r",stdin);
    while( scanf("%lf %lf",&p[n].x,&p[n].y) != EOF )
    {
        n 
++;
    }
    sort(p
+1,p+n,cmp1);

    sort(p
+1,p+n,cmp2);

    
int top = -1,S[MAXN];
    S[
++top] = 0;
    S[
++top] = 1;
    
for( i = 2 ; i < n ; i ++ )//grahamScan扫描法
    {
        
while( xmult(p[i],p[S[top]],p[S[top-1]]) > 0 )
            top 
--;
        S[
++top] = i;
    }
    
for( i = 0 ; i <= top ; i ++ )
        printf(
"(%.lf,%.lf)\n",p[i].x,p[i].y);
    
return 0;
}

转载于:https://www.cnblogs.com/shenyaoxing/archive/2009/10/10/1580441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值