poj 2007 Scrambled Polygon(凸包Graham扫描线模板)

Scrambled Polygon

题目链接:http://poj.org/problem?id=2007

代码如下:

#include<stdio.h>
#include<limits.h>
#include<stdlib.h>
#include<string.h>
#include<deque>
#include<algorithm>
using namespace std;
typedef struct
{
	int x;
	int y;
}Point;
int n, temp;
Point top1, top2, s[105];
deque<Point> q;
bool comp2(Point a, Point b)	//按幅角排序
{
	if((a.x-top1.x)*(b.y-top1.y)-(a.y-top1.y)*(b.x-top1.x)>0)
		return 1;
	else if((a.x-top1.x)*(b.y-top1.y)-(a.y-top1.y)*(b.x-top1.x)==0 && abs(a.x-top1.x)<abs(b.x-top1.x))
		return 1;
	return 0;
}
int main(void)
{
	n=1;
	int ax,ay;
	ax = ay = INT_MAX;
	while(~scanf("%d%d", &s[n].x, &s[n].y))
	{
	    n++;
	    if(s[n].y<ay || s[n].y==ay && s[n].x<ax)    //每次记录最左下角的,先下后左
            temp = n, ay = s[n].y, ax = s[n].x;
	}
    top1 = s[temp]; //作为sort排序的比较标准
    sort(s+1, s+n, comp2);
    q.push_back(s[1]);
    q.push_back(s[2]);
    temp = 3;		
    while(temp<n)
    {
        top1 = q.back();
        q.pop_back();		//临时弹出用作判定
        top2 = q.back();
        if((top2.x-top1.x)*(s[temp].y-top1.y)-(top2.y-top1.y)*(s[temp].x-top1.x)<=0)
        {
            q.push_back(top1);		//符合条件,临时弹出的归队
            q.push_back(s[temp]);	//当前点入队
            temp++;			//下一个点作为当前点
        }
        //不符合条件丢弃
    }
    int flag=0;
    while(q.empty()==0)
    {
        top1 = q.front();
        if((top1.x==0 && top1.y==0) || flag)//根据题意,循环入栈出栈,判断从(0,0)开始输出
        {
            printf("(%d,%d)\n", top1.x, top1.y);
            q.pop_front();
            flag=1;
        }
        else if(!flag)
        {
            q.pop_front();
            q.push_back(top1);
        }
    }
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值