转载

Poj-1113 Wall

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.

Poj-1113 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

C++

9 100 200 400 300 400 300 300 400 300 400 400 500 400 500 200 350 200 200 200

Sample Output

C++

题意:给你一个城堡的定点,让你在城堡外面建一个围墙,围墙离城堡距离必须大于d,问最少的长度是多少

凸包+一个圆的周长

C++

#include <iostream> #include <math.h> #include <algorithm> #include <stdlib.h>  using namespace std;  #define pi acos(-1.0) #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps)  struct point{ double x, y; }p[1005], convex[1005];  double dis(point a, point b) {  return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }  //计算cross product (P1-P0)x(P2-P0) double xmult(point p1, point p2, point p0){  return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y); } //graham算法顺时针构造包含所有共线点的凸包,O(nlogn) point p1, p2; int graham_cp(const void* a, const void* b){  double ret = xmult(*((point*) a), *((point*) b), p1);  return zero(ret) ? (xmult(*((point*) a), *((point*) b), p2) > 0 ? 1 : -1) : (ret > 0 ? 1 : -1); } void _graham(int n, point* p, int& s, point* ch){  int i, k = 0;  for (p1 = p2 = p[0], i = 1; i<n; p2.x += p[i].x, p2.y += p[i].y, i++)   if (p1.y - p[i].y>eps || (zero(p1.y - p[i].y) && p1.x > p[i].x))    p1 = p[k = i];  p2.x /= n, p2.y /= n;  p[k] = p[0], p[0] = p1;  qsort(p + 1, n - 1, sizeof(point), graham_cp);  for (ch[0] = p[0], ch[1] = p[1], ch[2] = p[2], s = i = 3; i < n; ch[s++] = p[i++])   for (; s>2 && xmult(ch[s - 2], p[i], ch[s - 1]) < -eps; s--); }  int wipesame_cp(const void *a, const void *b) {  if ((*(point *) a).y < (*(point *) b).y - eps) return -1;  else if ((*(point *) a).y > (*(point *) b).y + eps) return 1;  else if ((*(point *) a).x < (*(point *) b).x - eps) return -1;  else if ((*(point *) a).x > (*(point *) b).x + eps) return 1;  else return 0; }  int _wipesame(point * p, int n) {  int i, k;  qsort(p, n, sizeof(point), wipesame_cp);  for (k = i = 1; i < n; i++)   if (wipesame_cp(p + i, p + i - 1) != 0) p[k++] = p[i];  return k; }  //构造凸包接口函数,传入原始点集大小n,点集p(p原有顺序被打乱!) //返回凸包大小,凸包的点在convex中 //参数maxsize为1包含共线点,为0不包含共线点,缺省为1 //参数dir为1顺时针构造,为0逆时针构造,缺省为1 //在输入仅有若干共线点时算法不稳定,可能有此类情况请另行处理! int graham(int n, point* p, point* convex, int maxsize = 1, int dir = 1){  point* temp = new point[n];  int s, i;  n = _wipesame(p, n);  _graham(n, p, s, temp);  for (convex[0] = temp[0], n = 1, i = (dir ? 1 : (s - 1)); dir ? (i < s) : i; i += (dir ? 1 : -1))   if (maxsize || !zero(xmult(temp[i - 1], temp[i], temp[(i + 1)%s])))    convex[n++] = temp[i];  delete []temp;  return n; }  int main() {  int n;  double d;  while (cin >> n >> d)  {   memset(p, 0, sizeof(p));   memset(convex, 0, sizeof(convex));   double dist = 0.0;   for (int i = 0; i < n; i++)   {    cin >> p[i].x >> p[i].y;   }   //cout << n << endl;   int size = graham(n, p, convex,0);   //cout << size << endl;   for (int i = 0; i < size - 1; i++)   {    dist += dis(convex[i], convex[i + 1]);   }   dist += dis(convex[size - 1], convex[0]);//别忘了起始到结束的长度   cout << (int) (dist + 2 * pi * d + 0.5) << endl;  } }
#include <iostream> #include <math.h> #include <algorithm> #include <stdlib.h> usingnamespacestd; #define pi acos(-1.0) #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) structpoint{doublex,y;}p[1005],convex[1005]; doubledis(pointa,pointb) {  returnsqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } //计算cross product (P1-P0)x(P2-P0) doublexmult(pointp1,pointp2,pointp0){  return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } //graham算法顺时针构造包含所有共线点的凸包,O(nlogn) point p1,p2; intgraham_cp(constvoid*a,constvoid*b){  doubleret=xmult(*((point*)a),*((point*)b),p1);  returnzero(ret)?(xmult(*((point*)a),*((point*)b),p2)>0?1:-1):(ret>0?1:-1); } void_graham(intn,point*p,int&s,point*ch){  inti,k=0;  for(p1=p2=p[0],i=1;i<n;p2.x+=p[i].x,p2.y+=p[i].y,i++)   if(p1.y-p[i].y>eps||(zero(p1.y-p[i].y)&&p1.x>p[i].x))    p1=p[k=i];  p2.x/=n,p2.y/=n;  p[k]=p[0],p[0]=p1;  qsort(p+1,n-1,sizeof(point),graham_cp);  for(ch[0]=p[0],ch[1]=p[1],ch[2]=p[2],s=i=3;i<n;ch[s++]=p[i++])   for(;s>2&&xmult(ch[s-2],p[i],ch[s-1])<-eps;s--); } intwipesame_cp(constvoid*a,constvoid*b) {  if((*(point*)a).y<(*(point*)b).y-eps)return-1;  elseif((*(point*)a).y>(*(point*)b).y+eps)return1;  elseif((*(point*)a).x<(*(point*)b).x-eps)return-1;  elseif((*(point*)a).x>(*(point*)b).x+eps)return1;  elsereturn0; } int_wipesame(point*p,intn) {  inti,k;  qsort(p,n,sizeof(point),wipesame_cp);  for(k=i=1;i<n;i++)   if(wipesame_cp(p+i,p+i-1)!=0)p[k++]=p[i];  returnk; } //构造凸包接口函数,传入原始点集大小n,点集p(p原有顺序被打乱!) //返回凸包大小,凸包的点在convex中 //参数maxsize为1包含共线点,为0不包含共线点,缺省为1 //参数dir为1顺时针构造,为0逆时针构造,缺省为1 //在输入仅有若干共线点时算法不稳定,可能有此类情况请另行处理! intgraham(intn,point*p,point*convex,intmaxsize=1,intdir=1){  point*temp=newpoint[n];  ints,i;  n=_wipesame(p,n);  _graham(n,p,s,temp);  for(convex[0]=temp[0],n=1,i=(dir?1:(s-1));dir?(i<s):i;i+=(dir?1:-1))   if(maxsize||!zero(xmult(temp[i-1],temp[i],temp[(i+1)%s])))    convex[n++]=temp[i];  delete[]temp;  returnn; } intmain() {  intn;  doubled;  while(cin>>n>>d)  {   memset(p,0,sizeof(p));   memset(convex,0,sizeof(convex));   doubledist=0.0;   for(inti=0;i<n;i++)   {    cin>>p[i].x>>p[i].y;   }   //cout << n << endl;   intsize=graham(n,p,convex,0);   //cout << size << endl;   for(inti=0;i<size-1;i++)   {    dist+=dis(convex[i],convex[i+1]);   }   dist+=dis(convex[size-1],convex[0]);//别忘了起始到结束的长度   cout<<(int)(dist+2*pi*d+0.5)<<endl;  } } 
正文到此结束
Loading...