题目链接:https://vjudge.net/problem/POJ-1113
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39281 | Accepted: 13418 |
Description
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
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
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
Source
题意:
给出一座城堡(由点勾勒出来),问用最短城墙将城堡包围起来,且城墙与城堡的距离不能小于L。
题解:
1.假设没有规定城堡与城墙的距离,那么城墙的最短长度即为城墙点集的凸包的周长。
2.再考虑回城墙与城堡的距离,那么就要把凸包的每条边往外垂直移动L距离。移动过后,每条边与相邻的边都没有了交点,这时,需要在两条边之间加一段圆弧,圆弧的半径即为L,而所有圆弧加起来就是一个完整的圆,为什么?如图:
再根据初中知识:任意多边形的外角和为360度。即可得出结论。
4.所以总长度为:凸包的周长+圆的周长。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; const double eps = 1e-;
const double PI = acos(-1.0);
int sgn(double x)
{
if(fabs(x)<eps) return ;
if(x<) return -;
else return ;
} struct Point
{
double x,y;
Point(){}
Point(double _x, double _y) {
x = _x; y = _y;
}
Point operator -(const Point &b)const{
return Point(x-b.x, y-b.y);
}
double operator *(const Point &b)const{
return x*b.x+y*b.y;
}
double operator ^(const Point &b)const{
return x*b.y-y*b.x;
}
}; Point list[MAXN];
int Stack[MAXN], top; double dis(Point a, Point b)
{
return sqrt((a-b)*(a-b));
} bool cmp(Point p1, Point p2)
{
double tmp = (p1-list[])^(p2-list[]);
if(sgn(tmp)>) return true;
else if(sgn(tmp)== && sgn(dis(p1,list[])-dis(p2,list[]))<=)
return true;
else return false;
} void Graham(int n)
{
Point p0;
int k = ;
p0 = list[];
for(int i = ; i<n; i++)
{
if(p0.y>list[i].y||(p0.y==list[i].y&&p0.x>list[i].x))
{
p0 = list[i];
k = i;
}
}
swap(list[k],list[]);
sort(list+,list+n,cmp);
if(n==)
{
top = ;
Stack[] = ;
return;
}
if(n==)
{
top = ;
Stack[] = ;
Stack[] = ;
return;
}
Stack[] = ;
Stack[] = ;
top = ;
for(int i = ; i<n; i++)
{
while(top> && sgn((list[Stack[top-]]-list[Stack[top-]])^(list[i]-list[Stack[top-]]))<=)
top--;
Stack[top++] = i;
}
} int main()
{
int L, n;
while(scanf("%d%d", &n,&L)!=EOF)
{
for(int i = ; i<n; i++)
scanf("%lf%lf",&list[i].x,&list[i].y); Graham(n);
double perimeter = ;
for(int i = ; i<top; i++)
perimeter += dis(list[Stack[i]],list[Stack[(i+)%top]]);
perimeter += 2.0*PI*L;
printf("%.0f\n", perimeter);
}
}