(hdu step 7.1.7)Wall(求凸包的周长——求将全部点围起来的最小凸多边形的周长)
题目:
Wall |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 119 Accepted Submission(s): 47 |
Problem 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.
This problem contains multiple test cases! The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks. The output format consists of N output blocks. There is a blank line between output blocks. |
Sample Input
1 9 100 |
Sample Output
1628 |
Source
Northeastern Europe 2001
|
Recommend
JGShining
|
题目分析:
求凸包的周长。再求图报的周长前,首先要做的是计算凸包——找到将全部点围起来的最小凸多边形。
对于找到凸包的算法,下面代码用的是graham算法,对这个算法不太熟悉的童鞋能够先看一下:
http://blog.csdn.net/hjd_love_zzt/article/details/44311333
代码例如以下:
/*
* g.cpp
*
* Created on: 2015年3月16日
* Author: Administrator
*/ #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; const double epsi = 1e-8;
const double pi = acos(-1.0);
const int maxn = 1001; struct PPoint{//结构体尽量不要定义成Point这样的,easy和C/C++本身中的变量同名
double x;
double y; PPoint(double _x = 0,double _y = 0):x(_x),y(_y){ } PPoint operator - (const PPoint& op2) const{
return PPoint(x - op2.x,y - op2.y);
} double operator^(const PPoint &op2)const{
return x*op2.y - y*op2.x;
}
}; inline int sign(const double &x){
if(x > epsi){
return 1;
} if(x < -epsi){
return -1;
} return 0;
} inline double sqr(const double &x){
return x*x;
} inline double mul(const PPoint& p0,const PPoint& p1,const PPoint& p2){
return (p1 - p0)^(p2 - p0);
} inline double dis2(const PPoint &p0,const PPoint &p1){
return sqr(p0.x - p1.x) + sqr(p0.y - p1.y);
} inline double dis(const PPoint& p0,const PPoint& p1){
return sqrt(dis2(p0,p1));
} int n;
double l;
PPoint p[maxn];
PPoint convex_hull_p0; inline bool convex_hull_cmp(const PPoint& a,const PPoint& b){
return sign(mul(convex_hull_p0,a,b)>0)|| sign(mul(convex_hull_p0,a,b)) == 0 && dis2(convex_hull_p0,a) < dis2(convex_hull_p0,b);
} /**
* 计算点集a[]的凸包b[]。当中点集a有n个元素
*/
int convex_hull(PPoint* a,int n,PPoint* b){
if(n < 3){//假设顶点数小于3,构不成一个凸包
//输出失败信息
printf("wrong answer ,cause of n smaller than 3\n");
return -1;
} int i;
for(i = 1 ; i < n ; ++i){//遍历点集中的每个点
//寻找最低点(所谓的最低点就是最靠左下角的点)
if(sign(a[i].x - a[0].x) < 0 || (sign(a[i].x - a[0].x) == 0 && sign(a[i].y < a[0].y) < 0 )){
swap(a[i],a[0]);
}
} convex_hull_p0 = a[0];
sort(a,a+n,convex_hull_cmp);//排序 int newn = 2;
b[0] = a[0];
b[1] = a[1]; /**
* 在剩下的点中不断前进,假设当前点在前进方向左側,
* 则将当前点进栈,否则将近期入栈的点出栈.知道当前点在前进方向的左側
*/
for(i = 2 ; i < n ; ++i){
while(newn > 1 && sign(mul(b[newn-1],b[newn-2],a[i])) >= 0){
newn--;
} b[newn++] = a[i];//江当前点进栈
} return newn;//返回栈顶指针
} int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d %lf",&n,&l); int i;
for(i = 0 ; i < n ; ++i){
scanf("%lf %lf",&p[i].x,&p[i].y);
} n = convex_hull(p,n,p);
p[n] = p[0]; double ans = 0;
for(i = 0 ; i < n ; ++i){//求凸包的周长
ans += dis(p[i],p[i+1]);
} ans += 2*pi*l;//加上外面围墙的周长 /**
* "."后面的是小数精度控制。这里由于是浮点型。则取零代表不显示小数点(取整)
* .不为零时代表最大小数位数
*/
printf("%.0lf\n",ans); if(t != 0){//每个输出后面都要跟一个换行
printf("\n");
}
} return 0;
}
(hdu step 7.1.7)Wall(求凸包的周长——求将全部点围起来的最小凸多边形的周长)的更多相关文章
- (hdu step 7.1.5)Maple trees(凸包的最小半径寻找掩护轮)
称号: Maple trees Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- (hdu step 7.1.6)最大三角形(凸包的应用——在n个点中找到3个点,它们所形成的三角形面积最大)
题目: 最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- poj 3348:Cows(计算几何,求凸包面积)
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6199 Accepted: 2822 Description ...
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- poj 1113:Wall(计算几何,求凸包周长)
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28462 Accepted: 9498 Description ...
- hdu 1348 Wall(凸包模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others) M ...
- POJ 1113 || HDU 1348: wall(凸包问题)
传送门: POJ:点击打开链接 HDU:点击打开链接 以下是POJ上的题: Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- POJ 1113 Wall 求凸包的两种方法
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31199 Accepted: 10521 Descriptio ...
随机推荐
- POJ 3114 Tarjan+Dijkstra
题意: 间谍在战争期间想要传递一份谍报回国,谍报可以在邮局之间传递,但这种传递是单向的,并且会少耗一些时间.但是如果两个邮局在同一个国家的话,那么谍报在这两个邮局之间传递是不消耗时间的.如果几个邮局发 ...
- .net MVC成长记录(二)
今天上班的任务完成了,接下来写一下博客,巩固一下,再学习一些新知识. 闲话不多说,我们言归正传.昨天讲到了如何mvc框架在微软下,已经变成了一个非常灵活非常‘干净’的开发框架了, 同时也讲述了如何创建 ...
- 打开手机摄像头扫描二维码或条形码全部操作(代码写的不好,请提出指教,共同进步,我只是一个Android的小白)
(1)下载二维码的库源码 链接:http://pan.baidu.com/s/1pKQyw2n 密码:r5bv 下载完成后打开可以看到 libzxing 的文件夹,最后添加进 Android Stu ...
- 图像局部显著性—点特征(SIFT为例)
基于古老的Marr视觉理论,视觉识别和场景重建的基础即第一阶段为局部显著性探测.探测到的主要特征为直觉上可刺激底层视觉的局部显著性--特征点.特征线.特征块. SalientDetection 已经好 ...
- 11.02 跳过表中n行
select x.enamefrom (select a.ename,(select count(*)from emp bwhere b.ename <=a.ename) as rnfrom e ...
- 集合运算(UNION)
表的加法 集合运算:就是满足统一规则的记录进行的加减等四则运算. 通过集合运算可以得到两张表中记录的集合或者公共记录的集合,又或者其中某张表中记录的集合. 集合运算符:用来进行集合的运算符. UNIO ...
- PHP共享内存
如何使用 PHP shmop 创建和操作共享内存段,使用它们存储可供其他应用程序使用的数据. 1. 创建内存段 共享内存函数类似于文件操作函数,但无需处理一个流,您将处理一个共享内存访问 ID.第一个 ...
- eoLinker上线两周年+ AMS V4.0 发布:全新UI界面,带来领先的API开发管理解决方案!
2018年7月,eoLinker 发布了<eoLinker AMS 2018年年中用户调研问卷>,前后经历一周的时间,共收集到超过1000份有效调查问卷.超过300个有效改进意见. eoL ...
- sqlserver系统表使用
SELECT s.table_catalog as 数据库名, o.name as 表名, c.name as 列名FROM INFORMATION_SCHEMA.TABLES s,--库 sys.o ...
- Lua的string库函数、lua中string的模式匹配
--****************Lua的string库函数****************** --1.string.byte --string.byte (s [, i [, j]]) --取出 ...