POJ 1113 Wall (凸包)
2016-08-27 17:01
337 查看
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
Sample Output
Hint
结果四舍五入就可以了
题意:给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,
并且墙与所有点的距离至少为L,求这个墙最小的长度。
分析:城堡围墙长度最小值 = 城堡顶点坐标构成的散点集的凸包总边长 + 半径为L的圆周长
参考 http://blog.csdn.net/lyy289065406/article/details/6648622 http://www.cnblogs.com/kuangbin/archive/2012/04/13/2445633.html
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem0(a) memset(a, 0, sizeof(a))
#define memi(a) memset(a, inf, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e5 + 5;
const int mod = 1e8;
#define M_PI 3.14159265358979323846
struct Point{
double x, y;
Point() { }
Point(double x, double y):x(x), y(y) { }
Point operator - (const Point& p) const {
return Point(x - p.x, y - p.y);
}
int operator * (const Point& p) const { // 点乘
return x * p.x + y * p.y;
}
int operator ^ (const Point& p) const { // 叉乘
return x * p.y - y * p.x;
}
bool operator < (const Point& p) const { // 重载 <运算符
return (x == p.x && y < p.y) || x < p.x;
}
};
int convex_hull(Point* p, Point* vex_p, int n)
{
sort(p, p + n);
int k = 0;
for (int i = 0; i < n; i++){
while (k > 1 && ((vex_p[k-1] - vex_p[k-2]) ^ (p[i] - vex_p[k-1])) <= 0) k--;
vex_p[k++] = p[i];
}
int t = k;
for (int i = n-2; i >= 0; i--){
while (k > t && ((vex_p[k-1] - vex_p[k-2]) ^ (p[i] - vex_p[k-1])) <= 0) k--;
vex_p[k++] = p[i];
}
return k-1;
}
double cal_dis(Point a, Point b){
double x = a.x - b.x;
double y = a.y - b.y;
return sqrt(x*x + y*y);
}
Point p[1005], vex_p[1005];
int n, m, L;
double ans;
int main(void)
{
// freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
int i, j;
while (~scanf("%d %d", &n, &L))
{
for (i = 0; i < n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
m = convex_hull(p, vex_p, n);
ans = 0;
for (i = 0; i < m-1; i++)
ans += cal_dis(vex_p[i], vex_p[i+1]);
ans += cal_dis(vex_p[m-1], vex_p[0]);
ans += 2 * pi * L;
printf("%d\n", (int)(ans+0.5));
}
return 0;
}
$(".MathJax").remove();
<
4000
/script>
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
结果四舍五入就可以了
题意:给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,
并且墙与所有点的距离至少为L,求这个墙最小的长度。
分析:城堡围墙长度最小值 = 城堡顶点坐标构成的散点集的凸包总边长 + 半径为L的圆周长
参考 http://blog.csdn.net/lyy289065406/article/details/6648622 http://www.cnblogs.com/kuangbin/archive/2012/04/13/2445633.html
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem0(a) memset(a, 0, sizeof(a))
#define memi(a) memset(a, inf, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e5 + 5;
const int mod = 1e8;
#define M_PI 3.14159265358979323846
struct Point{
double x, y;
Point() { }
Point(double x, double y):x(x), y(y) { }
Point operator - (const Point& p) const {
return Point(x - p.x, y - p.y);
}
int operator * (const Point& p) const { // 点乘
return x * p.x + y * p.y;
}
int operator ^ (const Point& p) const { // 叉乘
return x * p.y - y * p.x;
}
bool operator < (const Point& p) const { // 重载 <运算符
return (x == p.x && y < p.y) || x < p.x;
}
};
int convex_hull(Point* p, Point* vex_p, int n)
{
sort(p, p + n);
int k = 0;
for (int i = 0; i < n; i++){
while (k > 1 && ((vex_p[k-1] - vex_p[k-2]) ^ (p[i] - vex_p[k-1])) <= 0) k--;
vex_p[k++] = p[i];
}
int t = k;
for (int i = n-2; i >= 0; i--){
while (k > t && ((vex_p[k-1] - vex_p[k-2]) ^ (p[i] - vex_p[k-1])) <= 0) k--;
vex_p[k++] = p[i];
}
return k-1;
}
double cal_dis(Point a, Point b){
double x = a.x - b.x;
double y = a.y - b.y;
return sqrt(x*x + y*y);
}
Point p[1005], vex_p[1005];
int n, m, L;
double ans;
int main(void)
{
// freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
int i, j;
while (~scanf("%d %d", &n, &L))
{
for (i = 0; i < n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
m = convex_hull(p, vex_p, n);
ans = 0;
for (i = 0; i < m-1; i++)
ans += cal_dis(vex_p[i], vex_p[i+1]);
ans += cal_dis(vex_p[m-1], vex_p[0]);
ans += 2 * pi * L;
printf("%d\n", (int)(ans+0.5));
}
return 0;
}
$(".MathJax").remove();
<
4000
/script>
相关文章推荐
- POJ 1113 Wall 凸包
- nyoj 226 && hdu HDU 1348 && poj 1113 wall [凸包周长]
- POJ 1113 Wall (凸包求周长)
- poj 1113 Wall----凸包 (给自己的一句话: 没AC,没理由!)
- poj 1113 Wall凸包
- poj-1113-Wall(凸包问题)
- [poj 1113]Wall[凸包]
- POJ 1113 Wall (凸包周长)
- POJ训练计划1113_Wall(几何/凸包)
- poj 1113 wall 简单的凸包
- poj 1113 Wall (凸包)
- POJ 1113 Wall 求凸包的两种方法
- 凸包求法 POJ 1113 Wall 凸包
- poj 1113:Wall(计算几何,求凸包周长)
- POJ 1113 Wall (凸包)
- 简单几何(凸包) POJ 1113 Wall
- 【POJ】1113 Wall(凸包)
- POJ1113:Wall(凸包)
- 【解题报告】 POJ 1113 Wall -- 求凸包周长
- Wall(凸包POJ 1113)