原题:
http://acm.nyist.net/JudgeOnline/problem.php?pid=78
3步求凸包:
1,找到 y 坐标 最小的点------A
2,以A为基准点逆时针排序(叉积)
3,开始遍历(叉积)
# include <iostream>
# include <stack>
# include <string>
#include <math.h>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct Point{
int x, y;
bool operator < (const Point& other) const{
if (x != other.x)
return x < other.x;
return y < other.y;
}
}Point;
Point p[105];
int Cross(Point a,Point b,Point c){
return (b.x - a.x)*(c.y - a.y) - (c.x - a.x)*(b.y - a.y);
}
bool cmp(Point a,Point b){
return Cross(p[0], a, b)>0;
}
int main(){
int N;
int m, i, j;
scanf("%d", &N);
while (N--){
scanf("%d", &m);
for (i = 0; i < m; i++)
scanf("%d%d",&p[i].x,&p[i].y);
sort(p, p + m); //1,找到 y 坐标 最小的点------A
sort(p, p + m, cmp); //2,以A为基准点逆时针排序(叉积)
Point ans[105];
ans[0] = p[0];
ans[1] = p[1];
//3,开始遍历(叉积)
for (j = i = 2; i < m; i++){
while (j>1&&Cross(ans[j - 1], ans[j - 2], p[i])>0)
j--;
ans[j++] = p[i];
}
sort(ans, ans + j);
for (i = 0; i < j; i++) printf("%d %d\n", ans[i].x, ans[i].y);
}
return 0;
}