Wall HDU1348(计算几何+凸包)

WallHDU - 1348

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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
Sample Output
1628
分析:
关于输出不想多说,四舍五入就好了,这里其实就是求一个凸包,下面采用了Graham 扫描法,代码特别拿出来说一下:
void convex_holl(point* ch,int n,point* ps){
	sort(ps,ps+n);
	m=0;
	for(int i=0;i<n;i++){
		while(m>1&&cross(ch[m-1]-ch[m-2],ps[i]-ch[m-2])<=0)m--;
		ch[m++]=ps[i];
	}
	int k=m;
	for(int i=n-2;i>=0;i--){
		while(m>k&&cross(ch[m-1]-ch[m-2],ps[i]-ch[m-2])<=0)m--;
		ch[m++]=ps[i];
	}
}
ch求的是凸包,注意两个循环之后,m就是总的点数,但是由于最后一个点和第一个点是一样的(不能省略找最后一点的过程,因为找到最后一点之后,可能还会把之前的点弹出),其实只有m-1个点是有效的,关于这两个循环的起始终止条件都要仔细想想,为这个WA了半天。。。至于叉乘,不用多讲了,就是判断线段走向

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<iomanip>
using namespace std;
const double eps=1e-8;
const double pi=3.1415926535;
struct Vector{
	double x,y;
	Vector(){}
	Vector(double x,double y):x(x),y(y){}
	bool operator < (const Vector v){
		if(x==v.x)return y<v.y;
		return x<v.x;
	}
};
typedef Vector point;
Vector operator + (Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,Vector b){return Vector(a.x*b.x,a.y*b.y);}
Vector operator / (Vector a,Vector b){return Vector(a.x/b.x,a.y/b.y);}
double dot(Vector a,Vector b){
	return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
	return a.x*b.y-a.y*b.x;
}
double len(Vector a){
	return sqrt(dot(a,a));
}
point ch[1010],ps[1010];
int n,m;//m是凸包上的点数
double l;
void convex_holl(point* ch,int n,point* ps){
	sort(ps,ps+n);
	m=0;
	for(int i=0;i<n;i++){
		while(m>1&&cross(ch[m-1]-ch[m-2],ps[i]-ch[m-2])<=0)m--;
		ch[m++]=ps[i];
	}
	int k=m;
	for(int i=n-2;i>=0;i--){
		while(m>k&&cross(ch[m-1]-ch[m-2],ps[i]-ch[m-2])<=0)m--;
		ch[m++]=ps[i];
	}
}
int main(){
	int t;cin>>t;
	int ok=0;
	while(t--){
		if(ok){
			printf("\n");
		}
		else ok++;
		cin>>n;
		cin>>l;
		for(int i=0;i<n;i++){
			cin>>ps[i].x>>ps[i].y;
		}
		convex_holl(ch,n,ps);
		double ans=0;
		for(int i=0;i<m-1;i++){
			ans+=len(ch[i]-ch[i+1]);
		}
		/*for(int i=0;i<n;i++){
			cout<<ps[i].x<<' '<<ps[i].y<<endl;
		}*/
		ans+=2*pi*l;
		//cout<<m<<endl;
		/*for(int i=0;i<m;i++){
			cout<<ch[i].x<<' '<<ch[i].y<<endl;
		}*/
		//cout<<m<<endl;
		cout<<int(ans+0.5)<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41333528/article/details/80562593