1036 - Suspense!

Jan and Tereza live in adjoining buildings and their apartments face one another. For their school science project, they want to construct a miniature suspension bridge made of rope, string, and cardboard connecting their two buildings. Two pieces of identical-length rope form the main suspension cables, which are attached to the bottoms of their windows. The cardboard ``roadbed" of the bridge is held up by numerous strings tied to the main cables. The horizontal bridge roadbed lies exactly one meter below the lowest point of the ropes. For aesthetic reasons, the roadbed should be at least two meters below the lower edge of the lower of the two students' windows. The laws of physics dictate that each suspension rope forms a parabola.

While Jan and Tereza don't plan to walk on this model bridge, there is a serious problem: some of the occupants of the apartment buildings own pet cats, and others own pet birds. Jan and Tereza want to be sure that their bridge doesn't provide a way for a cat to reach a bird. Jan and Tereza have observed that a cat cannot jump as high as 0.5 meters, and will not jump down as far as 3 meters. So as long as the bridge roadbed lies at least 0.5 meters above the bottom of a cat's window, or at least 3 meters below the bottom of a cat's window, the cat will not jump onto it. Likewise, a cat that successfully jumps onto the roadbed will not be able to reach a bird's window if the roadbed lies at least 0.5 meters below the bottom of the bird's window, or at least 3 meters above the bottom of the bird's window. Cats are concerned only with reaching birds, and they do not worry about returning home.

The figure below shows Jan's apartment (``J") and Tereza's apartment (``T") with a rope joining the bottoms of their windows and the cardboard roadbed one meter below the lowest point of the rope. The cat on the second floor can reach the bird on the second floor using the bridge.

\epsfbox{p3001.eps}

You must write a program to determine how much rope Jan and Tereza need to construct each cable for a bridge that won't endanger any of the birds in their two buildings.

Input for your program will be: the distance between the two buildings, in meters; the floor numbers for Jan and Tereza (with the lowest, or ground floor in each building numbered 1), the kinds of pets living in all the floors up through Jan's floor, and the kinds of pets living in all the floors up through Tereza's floor. Your program must determine the length of the longest cable that can be used to suspend a bridge between the two buildings that does not permit any cat to reach a bird by means of the bridge. The roadbed of the bridge must lie at least 1 meter above the ground and must lie exactly one meter below the lowest point of the suspension cables. It must also lie at least two meters below the lower of the two windows of Jan and Tereza. All rooms in the buildings are exactly 3 meters tall; all windows are exactly 1.5 meters tall and the bottom of each window lies exactly 1 meter above the floor of each room.

Input 

The input will describe several cases, each of which has three lines. The first line will contain two positive integers j and t ( 2$ \le$jt$ \le$25) representing Jan's floor and Tereza's floor, and a real value d (1$ \le$d$ \le$25) representing the distance, in meters, between the buildings. The second line will contain juppercase letters l1l2,..., lj separated by whitespace. Letter lk is `B' if a bird lives on floor number kof Jan's building, `C' if a cat lives on floor number k, and `N' if neither kind of pet lives on floor number k. The third line similarly contains t uppercase letters representing the same kind of information for the floor s in Tereza's building. The last case is followed by a line containing three zeroes.

Output 

For each case, print the case number (1, 2, ...) and the largest value c such that two cables, each of length c, can be used to suspend a bridge from the lower edges of Jan's and Tereza's windows so that the bridge floor lies one meter below the lowest point in the cable, lies at least 1 meter above the ground, lies at least two meters below Jan and Tereza's windows, and does not allow a cat to reach a bird. The length should be rounded to three places following the decimal point. If no such bridge can be constructed, print `impossible'. Print a blank line between the output for consecutive cases. Your output format should imitate the sample output.

Sample Input 

4 3 5.0
N C N C
N B B
4 3 5.0
C B C C
B C B
0 0 0

Sample Output 

Case 1: 14.377

Case 2: impossible





#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
char ch;
int C,Tj,Tt,i,j,k;
double d,ans;
int a[30],b[30];

int in()
{
	scanf("%c",&ch);
	while(ch!='C' && ch!='B' && ch!='N')
		scanf("%c",&ch);
	if(ch=='C')
		return 1;
	else if(ch=='B')
		return 2;
	else return 0;
}

double calc_2(double a,double c,double x)
{
	return x/2*sqrt(a*x*x+c) + c/(2*sqrt(a))*log(x*sqrt(a)+sqrt(a*x*x+c));
}

double calc_1(double C)
{
	double A,B,a,b,c;
	if(Tt==Tj)
		B=d/2;
	else
	{
		a=(Tt-Tj)*3;b=2*d*(Tj*3-2-C);c=-d*d*(Tj*3-2-C);
		B=(-b+sqrt(b*b-4*a*c))/(2*a);
		if(B<0||B>d)
			B=(-b-sqrt(b*b-4*a*c))/(2*a);
	}
	A=(Tj*3-2-C)/(B*B);
	return calc_2(4*A*A,1,B)+calc_2(4*A*A,1,d-B);
}

int main()
{
	C=0;
	while(1)
	{
		scanf("%d %d %lf",&Tj,&Tt,&d);
		if(Tj==0&&Tt==0&&d<1e-6)
			break;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for(i=1;i<=Tj;i++)
			a[i]=in();
		for(i=1;i<=Tt;i++)
			b[i]=in();
		ans=-1;
		for(i=1;i<Tt&&i<Tj;i++)
		{
			if(i>1)
				if((a[i]!=1&&b[i]!=1)||(a[i-1]!=2&&b[i-1]!=2))
				{
					ans=calc_1(i*3-2-0.5+1);
					if(C) 
						printf("\n");
					printf("Case %d: %.3lf\n",++C,ans);
					break;
				}
				if((a[i]!=1 && b[i]!=1) || (a[i]!=2&&b[i]!=2))
				{
					if(C)
						printf("\n");
					ans=calc_1(i*3-2+1);
					printf("Case %d: %.3lf\n",++C,ans);
					break;
				}
				if((a[i+1]!=1&&b[i+1]!=1) || (a[i]!=2&&b[i]!=2))
				{
					if(C)
						printf("\n");
					ans=calc_1(i*3-2+0.5+1);
					printf("Case %d: %.3lf\n",++C,ans);
					break;
				}
		}
		if(ans<0)
		{
			if(C)
				printf("\n");
			printf("Case %d: impossible\n",++C);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不仅仅是寻找

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值