1029 - Heliport

In these fast-paced times, companies are investing in heliports to reduce travel time for their busy executives. The heliports are typically circular landing pads, constructed on the roofs of the companies' headquarters.

\epsfbox{p2994.eps}

You must write a program that finds the largest radius for a circular heliport that can be constructed on the flat roof of a building that is in the form of a simple polygon. Since this is merely the design phase of the construction effort, your program must find only the radius of the heliport. The maximum radius for a heliport in the diagram shown is 10.

Input 

The input file contains several test cases. Each test case consists of two lines. The first line consists of an even integer  n  (   4$ \le$n$ \le$20 ), which is the number of the sides of the building. The second line consists of  n  pairs of the form  (md ) , where  m  is an integer (   1$ \le$m$ \le$50 ) and  d  is a letter ( U R D L ). Assuming the roof is drawn on the Cartesian plane,  m  is the length of a roof boundary segment and  d  is the direction of that segment as you travel counterclockwise around the roof.  U R D , and  L  mean ``Up," ``Right," ``Down," and ``Left" respectively. The boundary segments of the roof, which are parallel to the  x  and  y axes, are given in counterclockwise order. The starting position is the origin (0, 0).

Input for the last test case is followed by a line consisting of the number 0.

Output 

For each test case, the output consists of a separate line containing the case number (starting with 1) and a real number (rounded to two digits after the decimal point) representing the radius of the heliport. Print a blank line between cases as shown in the sample output.

Sample Input 

4
2 R 2 U 2 L 2 D
10
10 R 10 U 10 L 10 U 10 R 5 U 30 L 20 D 20 R 5 D
0

Sample Output 

Case Number 1 radius is: 1.00

Case Number 2 radius is: 10.00




#include<cstdio>
#include<math.h>
const double eps=1e-6;
int x[25],y[25],n,i,len,px,py,cases;
double ra,rb,r;
char dr;

bool check(double ox,double oy)
{
	int i,s;
	s=0;
	for(i=0;i<n;i++)
		if(x[i]>ox && ((y[i]>oy)^(y[i+1]>oy))) s++;
	if(s%2==0)
		return false;
	for(i=0;i<n;i++)
	{
		if((x[i]-ox)*(x[i]-ox) + (y[i]-oy)*(y[i]-oy)<(r-eps)*(r-eps))
			return false;
		if(x[i]==x[i+1] && ((y[i]>oy)^(y[i+1]>oy))&&fabs(x[i]-ox)<r-eps)
			return false;
		if(y[i]==y[i+1]&&((x[i]>ox)^(x[i+1]>ox))&&fabs(y[i]-oy)<r-eps)
			return false;
	}
	return true;
}

bool ok()
{
	int i,j;
	double di,dd,mx,my,dx,dy;
	for(i=0;i<n;i++) if(x[i]==x[i+1])
		for(j=0;j<n;j++) if(y[j]==y[j+1])
		{
			if(check(x[i]+r,y[j]+r)) return true;
			if(check(x[i]+r,y[j]-r)) return true;
			if(check(x[i]-r,y[j]+r)) return true;
			if(check(x[i]-r,y[j]+r)) return true;
		}
	for(i=0;i<n;i++)
		for(j=0;j<n;j++) if(x[i]==x[i+1])
		{
			di=fabs(x[j]-(x[i]+r));
			if(di<r)
			{
				dd=sqrt(r*r-di*di);
				if(check(x[i]+r,y[j]+dd)) return true;
				if(check(x[i]+r,y[j]-dd)) return true;
			}
			di=(fabs(x[j]-(x[i]-r)));
			if(di<r)
			{
				dd=sqrt(r*r-di*di);
				if(check(x[i]-r,y[j]+dd)) return true;
				if(check(x[i]-r,y[j]-dd)) return true;
			}
		}
		else
		{
			di=fabs(y[j]-(y[i]+r));
			if(di<r)
			{
				dd=sqrt(r*r-di*di);
				if(check(x[j]+dd,y[i]+r)) return true;
				if(check(x[j]-dd,y[i]+r)) return true;
			}
			di=fabs(y[j]-(y[i]-r));
			if(di<r)
			{
				dd=sqrt(r*r-di*di);
				if(check(x[j]+dd,y[i]-r)) return true;
				if(check(x[j]-dd,y[i]-r)) return true;
			}
		}

		for(i=0;i<n-1;i++)
			for(j=i+1;j<n;j++)
			{
				mx=(x[i]+x[j])/2.0;
				my=(y[i]+y[j])/2.0;
				di=sqrt((x[i]-mx)*(x[i]-mx) + (y[i]-my)*(y[i]-my));
				if(di>0&&di<r)
				{
					dd=sqrt(r*r-di*di);
					dx=(my-y[i])/di*dd;
					dy=(x[i]-mx)/di*dd;
					if(check(mx+dx,my+dy)) return true;
					if(check(mx-dx,my-dy)) return true;
				}
			}
			return false;
}

int main()
{
	while(scanf("%d",&n)&&n)
	{
		if(cases)
			printf("\n");
		px=0;py=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d %c",&len,&dr);
			if(dr=='R')
				px+=len;
			if(dr=='L')
				px-=len;
			if(dr=='U')
				py+=len;
			if(dr=='D')
				py-=len;
			x[i]=px;
			y[i]=py;
		}
		ra=0;rb=999;
		while(rb-ra>eps)
		{
			r=(ra+rb)/2;
			if(ok())
				ra=r;
			else
				rb=r;
		}
		printf("Case Number %d radius is: %.2lf\n",++cases,r);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值