7-10 Saving James Bond - Easy Version

在电影《生死关头》中,詹姆斯·邦德被一群毒贩俘虏,被困在一个布满鳄鱼的湖中央小岛上。通过计算与鳄鱼间的距离,邦德能否在限定的距离内从一个鳄鱼跳到另一个,最终逃出生天?这不仅是一场生死逃亡,也是对算法和图遍历能力的一次考验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).

Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether or not he can escape.

Input Specification:

Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.

Output Specification:

For each test case, print in a line "Yes" if James can escape, or "No" if not.

Sample Input 1:

14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12

Sample Output 1:

Yes

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

No

这道题考察的主要是图的遍历-DFS的应用,需要注意的是Bond的第一跳与之后的其他跳不同,主要为所形成的圆的半径要加上湖中心岛的半径;其次,Bond的第一跳可能有多种选择,这些选择都有可能会产生逃生路径,所以使用DFS进行遍历的时候要遍历图中所有的连通集。具体代码实现为:

#include<stdio.h>
#include<math.h>

#define YES 1
#define NO 0

struct coordinate{  //鳄鱼坐标 
	int x;
	int y;
}crocodile[105];

int N,D;
int visited[105] = {0};  //标志某一个鳄鱼坐标是否被访问过,=0未被访问过 

void Save007();

int main()
{
	int i;
	
	scanf("%d %d",&N,&D);  //输入鳄鱼数量及007可以跳跃的最大距离
	for(i=0; i<N; i++){
		scanf("%d %d",&crocodile[i].x,&crocodile[i].y);
	}
	Save007();
	
	return 0;
}

int IsSafe(int v)
{
	int X_distant,Y_distant;
	
	X_distant = abs(crocodile[v].x)-50;
	Y_distant = abs(crocodile[v].y)-50;
	
	return (abs(X_distant) <= D || abs(Y_distant) <= D);
}

int Jump(int v,int w)
{
	return (sqrt(pow(crocodile[v].x-crocodile[w].x,2)+pow(crocodile[v].y-crocodile[w].y,2)) <= D);  //两个鳄鱼坐标之间的距离	
}

int DFS(int v)
{
	int w;
	int answer = NO;
	
	visited[v] = 1;  //表明w被访问过
	if ( IsSafe(v) )  answer = YES;
	else{
		for(w=0; w<N; w++){
			if( !visited[w] && Jump(v,w)){
				answer = DFS(w);
				if(answer == YES)  break; 
			}
		}
	}
	
	return answer;
}

int FirstJump(int v)
{
	return (sqrt(pow(crocodile[v].x,2)+pow(crocodile[v].y,2)) <= (D+15));
}

void Save007()
{
	int v;
	int answer = NO;
	
	for(v=0; v<N; v++){
		if( !visited[v] && FirstJump(v) ){
			answer = DFS(v);
			if(answer == YES)  break;
		}
	}
	if(answer == YES)  printf("Yes\n");
	else  printf("No\n");
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值