POJ 1113-Wall(凸包-Graham算法)

悠悠 2022-09-26 11:51 291阅读 0赞

Wall














Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35509   Accepted: 12107

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.
1113_1.gif
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

  1. 9 100
  2. 200 400
  3. 300 400
  4. 300 300
  5. 400 300
  6. 400 400
  7. 500 400
  8. 500 200
  9. 350 200
  10. 200 200

Sample Output

  1. 1628

Hint

结果四舍五入就可以了

Source

Northeastern Europe 2001

题目意思:

给出国王的N个城堡的坐标,围墙里城堡距离至少L,求最小的围墙长度,使得能把所有城堡包围起来。

解题思路:

求城堡的凸包。注意:凸包顶点拐角处是圆弧不是直角,所以每个顶点圆弧的长度之和为一整个圆的周长。

先两两点计算距离,再加上圆周长,最后四舍五入即可。

(因为没注意到top比凸包点数多一光荣贡献两血……)

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<vector>
  4. #include<cmath>
  5. #include<cstring>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<set>
  9. #include<queue>
  10. #define INF 0xfffffff
  11. const int MAXN = 1010;
  12. using namespace std;
  13. const double eps = 1e-8;
  14. const double PI = acos(-1.0);
  15. int sgn(double x)
  16. {
  17. if(fabs(x) < eps)return 0;
  18. if(x < 0)return -1;
  19. else return 1;
  20. }
  21. struct Point
  22. {
  23. double x,y;
  24. Point() {} Point(double _x,double _y)
  25. {
  26. x = _x;
  27. y = _y;
  28. }
  29. Point operator -(const Point &b)const
  30. {
  31. return Point(x - b.x,y - b.y);
  32. } //叉积
  33. double operator ^(const Point &b)const
  34. {
  35. return x*b.y - y*b.x; //点积
  36. }
  37. double operator *(const Point &b)const
  38. {
  39. return x*b.x + y*b.y; //绕原点旋转角度B(弧度值),后x,y的变化
  40. }
  41. void transXY(double B)
  42. {
  43. double tx = x,ty = y;
  44. x = tx*cos(B) - ty*sin(B);
  45. y = tx*sin(B) + ty*cos(B);
  46. }
  47. };
  48. /*
  49. * 求凸包,Graham算法
  50. * 点的编号0~n-1
  51. * 返回凸包结果Stack[0~top-1]为凸包的编号
  52. */
  53. Point list[MAXN];
  54. int Stack[MAXN],top;
  55. double dist(Point a,Point b)
  56. {
  57. return sqrt((a-b)*(a-b));
  58. }
  59. //相对于list[0]的极角排序
  60. bool _cmp(Point p1,Point p2)
  61. {
  62. double tmp = (p1-list[0])^(p2-list[0]);
  63. if(sgn(tmp) > 0) return true;
  64. else if(sgn(tmp) == 0 && sgn(dist(p1,list[0]) - dist(p2,list[0])) <= 0) return true;
  65. else return false;
  66. }
  67. void Graham(int n)
  68. {
  69. Point p0;
  70. int k = 0;
  71. p0 = list[0]; //找最下边的一个点
  72. for(int i = 1; i < n; i++)
  73. {
  74. if( (p0.y > list[i].y) || (p0.y == list[i].y && p0.x > list[i].x) )
  75. {
  76. p0 = list[i];
  77. k = i;
  78. }
  79. }
  80. swap(list[k],list[0]);
  81. sort(list+1,list+n,_cmp);//排序
  82. if(n == 1)
  83. {
  84. top = 1;
  85. Stack[0] = 0;
  86. return;
  87. }
  88. if(n == 2)
  89. {
  90. top = 2;
  91. Stack[0] = 0;
  92. Stack[1] = 1;
  93. return ;
  94. }
  95. Stack[0] = 0;
  96. Stack[1] = 1;
  97. top = 2;
  98. for(int i = 2; i < n; i++)
  99. {
  100. while(top > 1 && sgn((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0)
  101. top--;
  102. Stack[top++] = i;
  103. }
  104. }
  105. int main()
  106. {
  107. int n,l;
  108. while(scanf("%d%d",&n,&l)!=EOF)
  109. {
  110. for(int i=0; i<n; ++i)
  111. scanf("%lf%lf",&list[i].x,&list[i].y);
  112. Graham(n);
  113. double ans=0;
  114. for(int i=0; i<top-1; ++i)
  115. ans+=fabs(dist(list[Stack[i]],list[Stack[i+1]]));
  116. ans+=fabs(dist(list[Stack[0]],list[Stack[top-1]]));
  117. ans+=PI*2*l;
  118. //cout<<ans<<endl;
  119. //ans=int(ans+0.5);//四舍五入
  120. printf("%d\n",int(ans+0.5));//不做四舍五入而直接写成printf("%.0f\n",ans);也能AC
  121. }
  122. return 0;
  123. }
  124. /*
  125. 9 100
  126. 200 400
  127. 300 400
  128. 300 300
  129. 400 300
  130. 400 400
  131. 500 400
  132. 500 200
  133. 350 200
  134. 200 200
  135. */

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读