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;
}