| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 21867 | Accepted: 7174 |
Description

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
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
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;
}*/
本文介绍了一个计算围绕不规则形状城堡所需建造墙壁最小长度的问题,通过寻找凸包并结合几何方法得出最优解。
220

被折叠的 条评论
为什么被折叠?



