nyoj-78-圈水池(Graham算法求凸包)

时间:2022-02-24 14:10:11

题目链接

 /*
Name:nyoj-78-圈水池
Copyright:
Author:
Date: 2018/4/27 9:52:48
Description:
Graham求凸包
zyj大佬的模板,改个输出就能用
*/
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct point{
double x, y;
};
bool mult(point sp, point ep, point op){ //叉积
return (sp.x - op.x) * (ep.y - op.y) >= (ep.x - op.x) * (sp.y - op.y);
}
inline bool operator < (const point &l, const point &r){
return l.y < r.y || (l.y == r.y && l.x < r.x);
}
int graham(int n, point pnt[], point res[]){
int i, len, top = ;
sort(pnt, pnt + n);//排序找最小的点
if (n == ){
return ;
}
res[] = pnt[];
if (n == ){
return ;
}
res[] = pnt[];
if (n == ){
return ;
}
res[] = pnt[];
for (i = ; i < n; i++){
while (top && mult(pnt[i], res[top], res[top - ])){
top--;
}
res[++top] = pnt[i];
}
len = top;
res[++top] = pnt[n - ];
for (i = n - ; i >= ; i--){
while (top != len && mult(pnt[i], res[top], res[top - ])){
top--;
}
res[++top] = pnt[i];
}
return top; // 返回凸包中点的个数
}
bool cmp(const point &l, const point &r){
return l.x < r.x || (l.x == r.x && l.y < r.y);
}
int main()
{
int t;
cin>>t;
point pnt[], res[];
while (t--) {
memset(res, , sizeof(res));
memset(pnt, , sizeof(pnt));
int m;
cin>>m;
for (int i=; i<m; i++)
cin>>pnt[i].x>>pnt[i].y;
int ct = graham(m, pnt, res);
sort(res, res + ct, cmp);
for (int i=; i<ct; i++) {
printf("%.0f %.0f\n", res[i].x, res[i].y);
}
}
return ;
}