代码改变世界

poj 1113 Wall----凸包 (给自己的一句话: 没AC,没理由!)

2012-05-17 14:22  java环境变量  阅读(262)  评论(0编辑  收藏  举报
Wall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21867   Accepted: 7174

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.

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

 

                     求一个 凸包,  再加一个 整圆,   为什么是加一个 圆 我也是 根据 图猜的.

            关于凸包,  先找一个 x,y 最小的点,也就是左下方的一个点作为起点 , 然后开始扫描,  扫描出 以起点为原点  逆时针方向 上的 第一个点 ,然后 以这个点为起点 继续扫描下去, 直到回到 起点, 扫描 完毕, 凸包就画出来了.

    ps :   关于这题,  有很深的感慨 ,下面 有张图   .   很久以前 就写了这题,  当时 想了很久 , 但一直 是WA,,,  一度还认为 题目有问题,   这就是 浮躁的 表现!!!    今天 整理 了下思路 , 静下心来  就AC了...  就是 把 一个n-1 改为 n就过了..      自己一定要吸取教训 ,从平时开始 就要 训练自己的心态 ,   当反复WA 时,  不要浮躁   ,可以缓一缓,但是 不能说什么 " 自己想的测试 数据都过了,总是WA,这题有问题吧"  来满足自己的虚荣心  ...只要WA,不要找借口,        只有AC 才是最有力的证明!!!

 

 

下面 是代码:    

//Memory: 204K  Time: 32MS 
//Language: C++  Result: Accepted 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
const double PI=3.141592653;
struct point
{
	double x,y;
}p[1005]; 

int cmp(const void *a,const void *b);
//int dblcmp(double a);
int graham(point base, point a, point b);

double dis(point a, point b);

int main()
{
//	freopen("in.txt","r",stdin);
	int n,i,j,vir[1005];
	double r,sum;
	point base;
	while(scanf("%d%lf",&n,&r)!=EOF)
	{
		memset(vir,0,sizeof(vir));
		sum=0;
		for(i=0;i<n;i++)
			scanf("%lf%lf", &p[i].x, &p[i].y);
		qsort(p, n, sizeof(point), cmp);
	//	for(i=0;i<n;i++) printf("%.0lf %.lf\n",p[i].x,p[i].y);
		base=p[0]; 
		int rIndex=1;
		for(j=0;j<n;j++)           //之前一直WA 的原因就是这个循环的条件 写成了 j<n-1
		{
			if(rIndex==0) break;
			for(i=0;i<n;i++)
			{
				if(vir[i] || rIndex==i ) continue;
				if(graham(base, p[rIndex] , p[i]))
					rIndex=i;
			}
			vir[rIndex]=1;
			sum+=dis(base, p[rIndex]);
			base=p[rIndex];
		
		//	printf("%d %.1lf  ",rIndex,sum);
		}
		double result=sum+PI*r*2;
		printf("%.0lf\n",result);
	}
	return 0;
}

int cmp(const void *a,const void *b)
{
	point *aa=(point *)a;
	point *bb=(point *)b;
	if(aa->y!=bb->y)
		return aa->y - bb->y;
	else  return aa->x - bb->x;
}

int graham(point base, point a, point b)
{
	double x1,x2,y1,y2;
	x1= a.x - base.x;
	x2= b.x - base.x;
	y1= a.y - base.y;
	y2= b.y - base.y;
	if( x1*y2 - x2*y1<=1e-6) return 1;
	else return 0;
}

double dis(point a, point b)
{
	double d=sqrt( (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y) );
	return d;
}

/*int dblcmp(double a)
{
	if(fabs(a)<=1e-6) return 1;
	else return 0;
}*/