poj 1113:Wall(计算几何,求凸包周长)
Wall
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28462 | Accepted: 9498 |
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.
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.
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
结果四舍五入就可以了
Source
计算几何,求凸包周长。
poj上的一道求凸包的经典题,和hdu上 hdu 1348:Wall(计算几何,求凸包周长) 一样,只不过输入输出格式有些不同。代码拿过来改了改就A了。
思路见以上博客。
本题需注意的是,要求四舍五入,最后在强制转换之前+0.5即可。另外,用C++的方式输入输出(cin,cout)可能会超时。
代码:
1 #include <iostream>
2 #include <cmath>
3 #include <iomanip>
4 #include <stdio.h>
5 using namespace std;
6 struct Point{
7 double x,y;
8 };
9 double dis(Point p1,Point p2)
10 {
11 return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
12 }
13 double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
14 {
15 return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
16 }
17 double graham(Point p[],int n) //点集和点的个数
18 {
19 int pl[10005],i;
20 //找到纵坐标(y)最小的那个点,作第一个点
21 int t = 1;
22 for(i=1;i<=n;i++)
23 if(p[i].y < p[t].y)
24 t = i;
25 pl[1] = t;
26 //顺时针找到凸包点的顺序,记录在 int pl[]
27 int num = 1; //凸包点的数量
28 do{ //已确定凸包上num个点
29 num++; //该确定第 num+1 个点了
30 t = pl[num-1]+1;
31 if(t>n) t = 1;
32 for(i=1;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。
33 double x = xmulti(p[i],p[t],p[pl[num-1]]);
34 if(x<0) t = i;
35 }
36 pl[num] = t;
37 } while(pl[num]!=pl[1]);
38 //计算凸包周长
39 double sum = 0;
40 for(i=1;i<num;i++)
41 sum += dis(p[pl[i]],p[pl[i+1]]);
42 return sum;
43 }
44 const double PI = 3.1415927;
45 int main()
46 {
47 int N,i;
48 double L;
49 Point p[1005];
50 cout<<setiosflags(ios::fixed)<<setprecision(0);
51 while(scanf("%d%lf",&N,&L)!=EOF){
52 for(i=1;i<=N;i++)
53 scanf("%lf%lf",&p[i].x,&p[i].y);
54 printf("%d\n",int(graham(p,N)+2*PI*L+0.5));
55 }
56 return 0;
57 }
Freecode : www.cnblogs.com/yym2013