Wall(hdu 1348)

套用凸包的模板

要注意调用求叉积的函数multi时 所传参数的顺序……在这死了好一会……

#include 
#include 
#include 
#define pi atan(1.0) * 4
using namespace std;

struct point
{
	int x, y;
}p[1100], stack[1100];
int top;

double dis(point a, point b)  
{//计算两点之间距离   
    return (sqrt(1.0 * ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))));  
}  

int multi(point a, point b, point c)
{
	return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x));
}

int cmp(point a, point b)
{
	int m = multi(a, b, p[0]);
	if(m > 0)
		return 1;
	if(m == 0 && dis(a, p[0]) < dis(b, p[0]))
		return 1;//
	return 0;
}


void input(int n)
{
	int k = 0;
	for(int i = 0; i < n; i++)
	{
		scanf("%d %d", &p[i].x, &p[i].y);
		if(p[i].x < p[k].x || (p[i].x == p[k].x && p[i].y < p[k].y))
			k = i;
	}
	point temp = p[k];
	p[k] = p[0];
	p[0] = temp;
	sort(p + 1, p + n, cmp);
}

void graham(int n)
{
	stack[0] = p[0];
	stack[1] = p[1];
	stack[2] = p[2];
	top = 2;
	for(int i = 3; i < n; i++)
	{
		while(top > 0 && multi(stack[top - 1], p[i], stack[top]) > 0)
			top --;
		stack[++top] = p[i];
	} 
}

int main (void)
{
	int t;
	scanf("%d", &t);
	while(t --)
	{
		int n, l;
		scanf("%d %d", &n, &l);
		input(n);
		graham(n);
		double sum = 0;

		for(int i = 0; i < top; i++)
			sum += dis(stack[i], stack[i + 1]);
			
		sum += dis(stack[top], stack[0]);
		sum += 	2 * pi * l;
		printf("%.lf\n", sum);
		if(t)
			printf("\n");
	}
	return 0;
} 



你可能感兴趣的:(计算几何,HDU)