hdu 1348:Wall(计算几何,求凸包周长)
2014-02-08 20:24
627 查看
Wall
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2848 Accepted Submission(s): 811
[align=left]Problem Description[/align]
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.
![](http://acm.hdu.edu.cn/data/images/1348-1.gif)
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
[align=left]Sample Output[/align]
1628
[align=left]Source[/align]
Northeastern Europe 2001
[align=left]Recommend[/align]
JGShining | We have carefully selected several similar problems for you: 1392 2150 1147 1558 2202
计算几何:凸包。
这道题是求凸包周长,思路是选定一个纵坐标(y)最小的点为凸包第一个点,然后遍历所有点通过比较叉积,求出在当前点逆时针方向的最后一个点为第二个点,之后依次类推求出构成凸包的点的顺序。
例如这道题提供的样例,构成凸包的点顺序依次为:
7 - 8 - 9 - 1 - 2 - 5 - 6 - 7
最后一个点回到起点,这就构成了一个凸包。
思路详见:
http://dev.gameres.com/Program/Abstract/Geometry.htm#凸包的求法
之后根据两点间的距离公式求出凸包周长,这道题还要再加上国王周围一个圆的周长(圆半径为L)。
注意输出不需要浮点部分,直接控制输出浮点数位数为0。
自己写的模板(求凸包周长):
struct Point{ double x,y; }; double dis(Point p1,Point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向 { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } double graham(Point p[],int n) //点集和点的个数 { int pl[10005]; //找到纵坐标(y)最小的那个点,作第一个点 int t = 1; for(int i=1;i<=n;i++) if(p[i].y < p[t].y) t = i; pl[1] = t; //顺时针找到凸包点的顺序,记录在 int pl[] int num = 1; //凸包点的数量 do{ //已确定凸包上num个点 num++; //该确定第 num+1 个点了 t = pl[num-1]+1; if(t>n) t = 1; for(int i=1;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。 double x = xmulti(p[i],p[t],p[pl[num-1]]); if(x<0) t = i; } pl[num] = t; } while(pl[num]!=pl[1]); //计算凸包周长 double sum = 0; for(int i=1;i<num;i++) sum += dis(p[pl[i]],p[pl[i+1]]); return sum; }
本题代码:
#include <iostream> #include <cmath> #include <iomanip> using namespace std; struct Point{ double x,y; }; double dis(Point p1,Point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向 { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } double graham(Point p[],int n) //点集和点的个数 { int pl[10005]; //找到纵坐标(y)最小的那个点,作第一个点 int t = 1; for(int i=1;i<=n;i++) if(p[i].y < p[t].y) t = i; pl[1] = t; //顺时针找到凸包点的顺序,记录在 int pl[] int num = 1; //凸包点的数量 do{ //已确定凸包上num个点 num++; //该确定第 num+1 个点了 t = pl[num-1]+1; if(t>n) t = 1; for(int i=1;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。 double x = xmulti(p[i],p[t],p[pl[num-1]]); if(x<0) t = i; } pl[num] = t; } while(pl[num]!=pl[1]); //计算凸包周长 double sum = 0; for(int i=1;i<num;i++) sum += dis(p[pl[i]],p[pl[i+1]]); return sum; } const double PI = 3.1415927; int main() { int T,N; double L; Point p[1005]; cin>>T; cout<<setiosflags(ios::fixed)<<setprecision(0); while(T--){ cin>>N>>L; for(int i=1;i<=N;i++) cin>>p[i].x>>p[i].y; cout<<graham(p,N)+2*PI*L<<endl; if(T) cout<<endl; } return 0; }
Freecode : www.cnblogs.com/yym2013
相关文章推荐
- hdu 1348 wall (计算几何,凸包)
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
- HDU 1348 Wall 凸包的周长
- [HDU 1348] Wall 凸包周长
- HDU 1348 wall 凸包模板题 求周长
- poj 1113:Wall(计算几何,求凸包周长)
- 计算几何 ( 凸包 )——Wall ( HDU 1248 )
- HDU 1392 Surround the Trees(计算几何,求凸包周长)
- POJ 1113 && HDU 1348 Wall (凸包周长 graham扫描法)
- POJ 1113 Wall(计算几何--凸包的周长)
- 计算几何:极角排序(poj 2007 Scrambled Polygon)与简单凸包(poj 1113 Wall)
- HDU 4667 Building Fence 计算几何 凸包+圆
- POJ 1113 Wall (计算几何-凸包)
- [hdu 1348] Wall · 凸包
- POJ1113 计算几何--整形凸包模板周长
- hdu 1348 (凸包求周长)
- HDU 1348 Wall 标准凸包 Graham-Scan算法
- POJ 1113 || HDU 1348: wall(凸包问题)
- hdu-1348 Wall(凸包)
- POJ 1113 Wall (计算几何-凸包)