POJ 1113:Wall

Wall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32541   Accepted: 11049

Description

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall. 

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements. 

The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.

Input

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle. 

Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

Sample Input

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200

Sample Output

1628

Hint

结果四舍五入就可以了

题意是国王要建一个围墙,环住他的城堡。顺时针给出城堡外面的点,要求城墙到城堡的距离必须大于等于给定的L。问城墙的最短距离,结果四舍五入。

周长=凸包的周长+以L为半径的周长。听说有一个推导公式,我也不知道是怎么来的这么一个结论。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

struct no
{
	double x, y;
}node[10005];

const double pi = 3.141592654;
int n;
int L;
int conbag[10005];

no orign;//原点

double dis(no n1, no n2)
{
	return (n1.x - n2.x)*(n1.x - n2.x) + (n1.y - n2.y)*(n1.y - n2.y);
}

double dis2(no n1, no n2)
{
	return sqrt((n1.x - n2.x)*(n1.x - n2.x) + (n1.y - n2.y)*(n1.y - n2.y));
}

double xmult(double x1, double y1, double x2, double y2)
{
	return x1*y2 - x2*y1;
}

double Across(no n1, no n2, no n3, no n4)
{
	return xmult(n2.x - n1.x, n2.y - n1.y, n4.x - n3.x, n4.y - n3.y);
}

int cmp(const void* n1, const void* n2)
{
	int temp = Across(orign, *(no *)n1, orign, *(no *)n2);

	if (temp > 0)
	{
		return -1;
	}
	else if (temp == 0)
	{
		return (dis(orign, *(no *)n1) - dis(orign, *(no *)n2));
	}
	else
	{
		return 1;
	}
}

int main()
{
	int i, j, k, min_x, pos_x;
	double sum;
	while (cin>>n>>L)
	{
		min_x = 100005;
		for (i = 1; i <= n; i++)
		{
			cin >> node[i].x >> node[i].y;
			if (node[i].x < min_x)
			{
				min_x = node[i].x;
				pos_x = i;
			}
			else if (min_x == node[i].x&&node[i].y < node[pos_x].y)
			{
				pos_x = i;
			}
		}
		orign = node[pos_x];
		qsort(node + 1, n, sizeof(no), cmp);

		int pc = 1;
		conbag[1] = 1;
		conbag[++pc] = 2;
		conbag[0] = 2;

		i = 3;
		while (i <= n)
		{
			if (Across(node[conbag[pc - 1]], node[conbag[pc]], node[conbag[pc]], node[i]) >= 0)
			{
				conbag[++pc] = i++;
				conbag[0]++;
			}
			else
			{
				pc--;
				conbag[0]--;
			}
		}
		sum = dis2(node[1], node[conbag[conbag[0]]]);
		for (i = 1; i + 1 <= conbag[0]; ++i)
		{
			sum += dis2(node[conbag[i]],node[conbag[i+1]]);
		}
		sum += pi * 2.0 * L;
		cout << (int)(sum + 0.5) << endl;
	}
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4899568.html