POJ 2007 Scrambled Polygon

POJ_2007

    这个题目本质上还是求凸包,然后按题目指定顺序输出各个点即可。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define MAXD 60
#define zero 1e-8
struct point
{
int x, y;
}p[MAXD], res[MAXD];
int N, P;
int cmp(const void *_p, const void *_q)
{
point *p = (point *)_p, *q = (point *)_q;
if(p->y == q->y)
return p->x - q->x;
return p->y - q->y;
}
int det(int x1, int y1, int x2, int y2)
{
return x1 * y2 - x2 * y1;
}
void init()
{
int i, j, k;
N = 0;
while(scanf("%d%d", &i, &j) == 2)
p[N].x = i, p[N].y = j, ++ N;
qsort(p, N, sizeof(p[0]), cmp);
}
int del(int top, int i)
{
if(det(res[top].x - res[top - 1].x, res[top].y - res[top - 1].y, p[i].x - res[top].x, p[i].y - res[top].y) < 0)
return 1;
return 0;
}
int graham()
{
int i, j, k, top = 1, mint;
res[0] = p[0], res[1] = p[1];
for(i = 2; i < N; i ++)
{
while(top && del(top, i))
-- top;
res[++ top] = p[i];
}
mint = top;
res[++ top] = p[N - 2];
for(i = N - 3; i >= 0; i --)
{
while(top != mint && del(top, i))
-- top;
res[++ top] = p[i];
}
return top;
}
void solve()
{
int i, j, k;
P = graham();
for(i = 0; ; i ++)
if(res[i].x == 0 && res[i].y == 0)
break;
printf("(0,0)\n");
for(j = (i + 1) % P; j != i; j = (j + 1) % P)
printf("(%d,%d)\n", res[j].x, res[j].y);
}
int main()
{
init();
solve();
return 0;
}


你可能感兴趣的:(2007)