1071 - The Hare and the Hounds


hare and hounds road rally requires contestants (the ``hounds") to identify a route of one or more roads selected by the organizer (the ``hare"). Both parties move over roads that meet at various intersections. Upon entering any intersection (except special ones to be described shortly), both the hound and the hare select routes using the main road rule. The main road rule is always ``straight as possible," meaning make the smallest turn (perhaps none) necessary to continue. If there are two such choices possible (such as at some ``Y" intersections), the main road rule dictates the rightmost (from the point of view of the hound) of the two acceptable alternatives should be selected.

At certain intersections, the hare may violate the main road rule by taking a random road away from the intersection (though never the original road used to reach the intersection). These intersections are marked by the hare as choice points (usually by a colored mark on the pavement in the intersection). When reaching a choice point, the hound must try each road leaving the intersection and travel that road (potentially traveling through other intersections) until reaching a confirmation marker (usually some flour dumped by the hare on the road) confirming the correct route selection. An incorrect route selection is indicated by one of the following:

  • The hound travels a specified maximum distance from the choice point before reaching a confirmation marker.
  • The hound reaches a ``dead end" (an ``intersection" with only one road to it) before reaching the confirmation marker.
  • The hound reaches a choice point before reaching the confirmation marker. (The hare always places a confirmation marker on the route following a choice point. Also, the hare never returns to a previous choice point.)

After detecting an incorrect route, the hound must trace back along the route taken to the choice point and select a different route alternative. If the hound encounters the endpoint while looking for a confirmation marker, he ignores it.

When selecting routes from a choice point, the hound uses the main road rule in a slightly different way. The first road selected is the same as if the intersection were not a choice point. But if the hound must return to the choice point (i.e., the first road taken from the choice point was not part of the hare's route), then the hound chooses the second road using the main road rule from the direction with which he returns to the choice point (ignoring any direction that he has already tried as well as the direction that he originally arrived from). This process repeats each time the hound returns to the choice point until he finds the proper route. This multiple use of the main road rule occurs only at choice points. For the purposes of this problem, the hound will not remember the result of traveling down any road, even if he returns to it multiple times while exploring one choice point.

For this problem you will be given a road map (a configuration of roads and intersections), the list of choice point intersections, the placement of confirmation markers, the maximum distance from a choice point to a confirmation marker, the starting and ending intersections (neither of which will be choice points), and the direction to be used in leaving the starting intersection. Using this information, you will simulate the hound's search for the hare's route. You may assume that the hound, using the strategy described, will always discover and trace the hare's route.

Input 

There may be multiple input test cases. The data for each case begins with a line containing 7 integers: ncp(number of choice point intersections), nroad (number of roads, never larger than 150), ncm (number of confirmation markers, never more than 100), confdist (maximum distance from a choice point intersection to the confirmation marker, at most 2000), startisect (starting intersection number), endisect (ending intersection number), and startdir (the direction of the starting road). Intersections are identified using integers between 1 and 100.

The starting line for each case is immediately followed by a line containing ncp integers giving the identifying numbers of the choice point intersections.

Next there are nroad lines. Roads are identified using sequential integers starting with 1, matching the order in which their specification lines appear in the input. Each such line contains 5 integers: the identifying numbers of the two intersections connected by the road, the compass directions (from 0 to 359 degrees, 0 is north, 90 is east) with which the road leaves the intersections, and the length of the road. Each road can be traveled in both directions and no two roads will enter an intersection at the same angle.

Finally there are ncm lines that identify the placement of the confirmation markers. Each of these lines contains three integers giving the identifying number of an intersection, the identifying number of a road leaving that intersection, and the distance from the intersection to the confirmation marker. Confirmation markers will not be placed at intersections. Confirmation markers also will not be dropped along roads that start/end in the same intersection.

Input for the last case is followed by a line containing 7 zeroes.

Output 

For each test case, print the case number (starting with 1), the length of the hare's route, the length of the hound's search (including all incorrect paths taken at choice points), and the road numbers in the hare's route in the order the hare traveled them, using the format shown in the sample data. Print a blank line after the output for each case.

Sample Input 

1 5 1 3 3 1 180 
2 
2 4 180 0 5 
1 4 180 90 6 
4 2 270 270 5 
3 2 180 0 8 
1 2 270 90 3 
4 3 3 
0 0 0 0 0 0 0

Sample Output 

Case 1: 
   Length of hare's route is 19 
   Length of hound's search is 31 
   Route: 4 3 2



#include<stdio.h>
#include<string.h>
#include<math.h>
const int maxn=105;
const int oo=1000000000;
int test,ncp,nroad,ncm1,confdist,startisect,endisect,startdir,i,j,k;
bool visit[360],chp[maxn];
int ans[1005],p[1005];
int marker[maxn][360],op[maxn][360],er[maxn][360],id[maxn][360],len[maxn][360];
int l1,l2,tot,pn;

bool init()
{
	int i,j,x,y,rx,ry,l;
	++test;
	scanf("%d%d%d%d%d%d%d",&ncp,&nroad,&ncm1,&confdist,&startisect,&endisect,&startdir);
	if(startisect==0)
		return false;
	memset(chp,0,sizeof(chp));
	for(i=1;i<=ncp;i++)
	{
		scanf("%d",&x);
		chp[x]=true;
	}
	memset(id,0,sizeof(id));
	for(i=1;i<=nroad;i++)
	{
		scanf("%d %d %d %d %d",&x,&y,&rx,&ry,&l);
		id[x][rx]=i;len[x][rx]=l;
		op[x][rx]=y;er[x][rx]=ry;
		id[y][ry]=i;len[y][ry]=l;
		op[y][ry]=x;er[y][ry]=rx;
	}
	for(i=1;i<=maxn;++i)
		for(j=0;j<=359;++j)
			marker[i][j]=oo;

	for(i=1;i<=ncm1;++i)
	{
		scanf("%d%d%d",&x,&y,&l);
		for(j=0;j<=359;++j)
			if(id[x][j]==y)
			{
				if(l<marker[x][j])
					marker[x][j]=l;
				if(len[x][j]-l<marker[op[x][j]][er[x][j]])
					marker[op[x][j]][er[x][j]]=len[x][j]-l;
			}
	}
	return true;
}

bool find(int &k,int &kdir,int &sum)
{
	int i,j,v,vdir,pre;
	sum=0;pn=0;v=k;vdir=kdir;
	while(1)
	{
		p[++pn]=id[v][vdir];
		if(marker[v][vdir]+sum<=confdist)
		{
			k=op[v][vdir];
			kdir=(er[v][vdir]+180)%360;
			sum+=len[v][vdir];
			return true;
		}
		if(len[v][vdir] + sum>confdist)
		{
			sum=confdist;
			return false;
		}
		sum+=len[v][vdir];
		pre=vdir;
		vdir=(er[v][vdir]+180)%360;
		v=op[v][pre];
		if(chp[v])
			return false;
		for(i=0;i<=180;++i)
		{
			if(i==180)
				return false;
			j=(vdir+i)%360;
			if(id[v][j]>0)
			{
				vdir=j;
				break;
			}
			j=(vdir+360-i)%360;
			if(id[v][j]>0)
			{
				vdir=j;
				break;
			}
		}
	}
	return false;
}

void work()
{
	int i,j,k,kdir,v,delta;
	k=startisect;
	kdir=startdir;
	l1=l2=tot=0;
	do
	{
		if(chp[k])
		{
			memset(visit,0,sizeof(visit));
			visit[(kdir+180)%360]=true;
			while(1)
			{
				v=-1;
				for(i=0;i<=180;++i)
				{
					j=(kdir+i)%360;
					if(id[k][j]>0 && !visit[j])
					{
						v=j;
						break;
					}
					j=(kdir+360-i)%360;
					if(id[k][j]>0 && !visit[j])
					{
						v=j;
						break;
					}
				}
				visit[v]=true;
				if(find(k,v,delta))
				{
					l1+=delta;
					l2+=delta;
					for(i=1;i<=pn;++i)
						ans[++tot]=p[i];
					kdir=v;
					break;
				}
				else
				{
					kdir=(v+180)%360;
					l2+=delta*2;
				}
			}
		}
		else
		{
			for(i=0;i<=180;++i)
			{
				j=(kdir+i)%360;
				if(id[k][j]>0)
				{
					l1+=len[k][j];
					l2+=len[k][j];
					ans[++tot]=id[k][j];
					kdir=(er[k][j] + 180)%360;
					k=op[k][j];
					break;
				}
				j=(kdir+360-i)%360;
				if(id[k][j]>0)
				{
					l1+=len[k][j];
					l2+=len[k][j];
					ans[++tot]=id[k][j];
					kdir=(er[k][j]+180)%360;
					k=op[k][j];
					break;
				}
			}
		}
	}while(k!=endisect);
	printf("Case %d:\n",test);
	printf("   Length of hare's route is %d\n",l1);
	printf("   Length of hound's search is %d\n",l2);
	printf("   Route:");
	for(i=1;i<=tot;++i)
		printf(" %d",ans[i]);
	printf("\n");
	printf("\n");
}

int main()
{
	test=0;
	while(init())
		work();
	return 0;
}


Sure, here is a possible implementation of the program in Java: ```java public class TortoiseAndHareRace { private static final int RACE_LENGTH = 1000; private static final int TORTOISE_SPEED = 1; // meter per 1500 milliseconds private static final int HARE_SPEED = 5; // meters per 500 milliseconds private static final int HARE_REST_DISTANCE = 700; private static final long HARE_REST_TIME = 10000; // milliseconds public static void main(String[] args) throws InterruptedException { int tortoiseDistance = 0; int hareDistance = 0; boolean hareIsResting = false; System.out.println("ON YOUR MARK, GET SET"); System.out.println("BANG!!!!"); System.out.println("AND THEY'RE OFF!!!!"); while (tortoiseDistance < RACE_LENGTH && hareDistance < RACE_LENGTH) { // Tortoise moves Thread.sleep(1500); tortoiseDistance += TORTOISE_SPEED; System.out.println("Tortoise runs 1 meter"); // Hare moves if (!hareIsResting) { Thread.sleep(500); hareDistance += HARE_SPEED; System.out.println("Hare runs 5 meters"); // Check if the hare needs to rest if (hareDistance >= HARE_REST_DISTANCE) { hareIsResting = true; System.out.println("Hare is resting"); Thread.sleep(HARE_REST_TIME); hareIsResting = false; System.out.println("Hare wakes up and continues running"); } } // Print the current distance System.out.println("Tortoise: " + tortoiseDistance + " meters"); System.out.println("Hare: " + hareDistance + " meters"); } // Print the race result if (tortoiseDistance >= RACE_LENGTH && hareDistance >= RACE_LENGTH) { System.out.println("It's a tie!"); } else if (tortoiseDistance >= RACE_LENGTH) { System.out.println("TORTOISE WINS!!!"); } else { System.out.println("Hare wins. Yuch."); } } } ``` This program uses two integer variables `tortoiseDistance` and `hareDistance` to keep track of the distances the tortoise and the hare have traveled, respectively. It also uses a boolean variable `hareIsResting` to indicate whether the hare is resting or not. The program starts by printing some messages to simulate the start of the race. It then enters a loop that simulates the race until one of the runners reaches the finish line (`RACE_LENGTH` meters). Inside the loop, the tortoise moves 1 meter every 1500 milliseconds, and the hare moves 5 meters every 500 milliseconds, except when it is resting. If the hare reaches the `HARE_REST_DISTANCE` (700 meters), it rests for `HARE_REST_TIME` (10000 milliseconds) before continuing to run. After each iteration of the loop, the program prints the current distances of the tortoise and the hare. When the loop ends, the program prints a message indicating the winner of the race. Note that the output result may vary slightly between runs due to the randomness in the timing of the threads.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值