1070 - Conveyor Belt

Many mechanical systems work with rotating shafts connected with conveyor belts. The shafts have a variety of sizes and rotate in either a clockwise or a counterclockwise manner. The exact way in which a belt will connect two shafts depends on their rotations, as shown in Figures 1 and 2.

\epsfbox{p4120a.eps}
\epsfbox{p4120b.eps}

One task in setting up such mechanical systems is to link together two given shafts, subject to these constraints:

  • If the two shafts being connected are too far apart, the belt may start to vibrate chaotically when perturbed slightly. To prevent this, you can connect shafts only when the distance between the points where the belt leaves one shaft and touches the other is less than some distance d (the exact value of d varies depending on the type of belt).
  • No belt can cross over itself, as shown in Figure 3.
  • No belt can pass through another shaft (or touch one rotating the wrong way), as shown in Figure 4.
  • The belt is not a loop; it goes in only one direction, from the starting shaft to the ending shaft. The starting shaft ``pushes'' the belt and the ending shaft ``pulls'' it.

As an example, consider the problem of connecting shaft A to shaft D in Figure 5. Suppose that the distance needed to connect A to C (shown in blue dashed line) or to connect B to D is greater than the limit allowed. Then the shortest distance to connect A to D is shown in solid line, going from shaft A to B to C and then D. Notice that the connection cannot go from B to E and then D as the belt would cross itself.

\epsfbox{p4120c.eps}

You must write a program that calculates the minimum length of the belt to connect the two given shafts.

Input 

The input consists of multiple test cases. Each test case starts with a line containing an integer N (1$ \le$N$ \le$20) indicating the number of shafts, numbered from 0 to N - 1 . Starting on the next line are N four-tuples of the form x y r s , where x and y are the integer coordinates of a shaft center, r is the integer radius of the shaft, and s is either ``C" for clockwise or ``CC" for counterclockwise (0$ \le$xy$ \le$10000 and 0 < r$ \le$1000) . Positive x is right and positive y is up. The first four-tuple specifies shaft 0, the second one shaft 1 and so on. These four-tuples may extend over multiple lines, though no four-tuple will be split across two lines. No two shafts touch or overlap. The last line of each test case contains two integers iand j indicating the starting and ending shafts, followed by a floating point value d specifying the maximum distance constraint.

The last test case is followed by a line containing a single zero.

Output 

For each test case, print the case number (starting with 1) followed by the length of the path of minimum distance. Print `Cannot reach destination shaft' if the destination shaft cannot be reached. Use the format shown in the sample output.

When measuring the distance, start where the belt first leaves the starting shaft and end where the belt first touches the ending shaft. Your distance calculation must include both the length of belt between shafts as well as the distance the belt travels around intermediate shafts. Your answer should be rounded to the nearest hundredth, though you need not print trailing zeroes after the decimal point.

Sample Input 

5 
24 50 14 C 93 78 20 C 118 8 15 CC 
167 32 13 C 159 88 15 CC
0 3 82.5 
5 

24 50 14 C 93 78 20 C 118 8 15 CC 
167 32 13 C 159 88 15 C
0 3 82.5 
5
24 50 14 C 93 78 20 C 118 8 15 CC 
167 32 13 C 159 88 15 C
0 3 8.5 
0

Sample Output 

Case 1: length = 271 
Case 2: length = 228.23 
Case 3: Cannot reach destination shaft





#include<stdio.h>
#include<math.h>
int x[20],y[20],r[20],st,ed,n,i,j,k,cases;
char dr[3],mark[20];
double dc[20][20],arg[20][20],len[20][20],xi[20][20],yi[20][20],xj[20][20],yj[20][20];
double pxa[20],pya[20],pxb[20],pyb[20],eps,pi,d,cur,ans;

double dis(double x,double y)
{
	return sqrt(x*x+y*y);
}

double angle(double x,double y)
{
	if(y>0)
		return acos(x/dis(x,y));
	if(y<0)
		return 2*pi-acos(x/dis(x,y));
	if(x>0)
		return 0;
	return pi;
}

int cross(double xa,double ya,double xb,double yb,double xc,double yc,double xd,double yd)
{
	double cra,crb;
	cra=(xb-xa)*(yc-ya) - (xc-xa)*(yb-ya);
	crb=(xb-xa)*(yd-ya) - (xd-xa)*(yb-ya);
	if(cra*crb>-eps)
		return 0;
	cra=(xd-xc)*(ya-yc) - (xa-xc)*(yd-yc);
	crb=(xd-xc)*(yb-yc) - (xb-xc)*(yd-yc);
	if(cra*crb>-eps)
		return 0;
	return 1;
}

void go(int v,double a)
{
	int j,k;
	double add;
	for(j=0;j<n;j++)
		if(mark[j]&&len[v][j]>0)
		{
			for(k=0;k<i;k++)
				if(cross(pxa[k],pya[k],pxb[k],pyb[k],xi[v][j],yi[v][j],xj[v][j],xj[v][j]))
					break;
			if(k<i)
				continue;
			pxa[i]=xi[v][j];
			pya[i]=yi[v][j];
			pxb[i]=xj[v][j];
			pyb[i]=yj[v][j];
			add=len[v][j];
			if(a>-1)
				if(r[v]<0)
				{
					if(arg[v][j]>a-eps)
						add-=r[v]*(arg[v][j]-a);
					else
						add-=r[v]*(2*pi+arg[v][j]-a);
				}
				else
				{
					if(a>arg[v][j]-eps)
						add+=r[v]*(a-arg[v][j]);
					else
						add+=r[v]*(2*pi+a-arg[v][j]);
				}
				mark[j]=0;
				cur+=add;
				i++;
				if(cur<ans)
					if(j==ed)
						ans=cur;
					else
						go(j,arg[v][j]);
				i--;
				cur-=add;
				mark[j]=1;
		}
}

int main()
{
	eps=1e-8;
	pi=acos(-1.0);
	while(scanf("%d",&n)&&n)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d%d%d %s",&x[i],&y[i],&r[i],dr);
			if(dr[1])
				r[i]=-r[i];
			mark[i]=1;
		}
		scanf("%d%d%lf",&st,&ed,&d);
		mark[st]=0;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				dc[i][j]=dis(x[j]-x[i],y[j]-y[i]);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++) if(i!=j)
			{
				arg[i][j]=angle(x[j]-x[i],y[j]-y[i]) + acos((r[i]-r[j])/dc[i][j]);
				if(arg[i][j]>=2*pi)
					arg[i][j]-=2*pi;
				if(arg[i][j]<0)
					arg[i][j]+=2*pi;
				len[i][j]=sqrt(dc[i][j]*dc[i][j] - (r[j]-r[i])*(r[j]-r[i]));
				if(len[i][j]>d)
					len[i][j]=-1;
				else
				{
					xi[i][j]=x[i]+r[i]*cos(arg[i][j]);yi[i][j]=y[i]+r[i]*sin(arg[i][j]);
					xj[i][j]=x[j]+r[j]*cos(arg[i][j]);yi[i][j]=y[j]+r[j]*sin(arg[i][j]);
					for(k=0;k<n;k++) if(k!=i&&k!=j)
					{
						if((xj[i][j]-xi[i][j])*(x[k]-xi[i][j]) + (yj[i][j]-yi[i][j])*(y[k]-yi[i][j])<=0)
							continue;
						if((xi[i][j]-xj[i][j])*(x[k]-xj[i][j]) + (yi[i][j]-yj[i][j])*(y[k]-yj[i][j])<=0)
							continue;
						if(fabs((xj[i][j]-xi[i][j])*(y[k]-yi[i][j]) - (yj[i][j]-yi[i][j])*(x[k]-xi[i][j]))/len[i][j] >labs(r[k])+eps)
							continue;
						len[i][j]=-1;
						break;
					}
				}
			}
		cur=0;
		ans=2e9;
		i=0;
		go(st,-2);
		printf("Cases %d: ",++cases);
		if(ans<1e9)
			if(int(ans*100+0.5)%10)
				printf("length = %2.lf\n",ans);
			else
				if(int(ans*100+0.5)%100)
					printf("length = %1.lf\n",ans);
				else
					printf("length = %0.lf\n",ans);
		else
			printf("Cannot reach destination shaft");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值