码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 1348 Wall 凸包

时间:2015-04-07 23:35:51      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:


Wall

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4001    Accepted Submission(s): 1131


Problem 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.

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
 

Source
 


/* ***********************************************
Author        :CKboss
Created Time  :2015年04月07日 星期二 21时12分04秒
File Name     :HDOJ1348.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const double eps=1e-7;

int dcmp(double x) { if(fabs(x)<eps) return 0; return (x<0)?-1:1; }

int n;
double r;

struct Point
{
	double x,y;
	Point(double _x=0,double _y=0):x(_x),y(_y){};
};

Point operator+(Point A,Point B) { return Point(A.x+B.x,A.y+B.y); }
Point operator-(Point A,Point B) { return Point(A.x-B.x,A.y-B.y); }
Point operator*(Point A,double p) { return Point(A.x*p,A.y*p); }
Point operator/(Point A,double p) { return Point(A.x/p,A.y/p); }

bool operator<(const Point& a,const Point& b) { return a.x<b.x||(a.x==b.x&&a.y<b.y); }
bool operator==(const Point& a,const Point& b) { return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; }


double Dot(Point A,Point B){return A.x*B.x+A.y*B.y;}
double Length(Point A) {return sqrt(Dot(A,A));}
double Cross(Point A,Point B) { return A.x*B.y-A.y*B.x; }

vector<Point> CovexHull(vector<Point>& p)
{
	sort(p.begin(),p.end());
	p.erase(unique(p.begin(),p.end()),p.end());
	int n=p.size();
	int m=0;
	vector<Point> ch(n+1);
	for(int i=0;i<n;i++)
	{
		while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
		ch[m++]=p[i];
	}
	int k=m;
	for(int i=n-2;i>=0;i--)
	{
		while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
		ch[m++]=p[i];
	}
	if(n>1) m--;
	ch.resize(m);
	return ch;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%lf",&n,&r);
		vector<Point> vp;
		for(int i=0;i<n;i++)
		{
			double X,Y;
			scanf("%lf%lf",&X,&Y);
			vp.push_back(Point(X,Y));
		}
		vector<Point> CH=CovexHull(vp);
		CH.push_back(CH[0]);
		double ans = 2.*acos(-1.0)*r;
		int sz=CH.size();
		for(int i=1;i<sz;i++)
		{
			double duan = Length(CH[i]-CH[i-1]);
			ans+=duan;
		}
		printf("%.0lf\n",ans);
		if(T_T) putchar(10);
	}
    
    return 0;
}



HDOJ 1348 Wall 凸包

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/44926411

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!