06-图2 Saving James Bond - Easy Version (25分)(数据结构)(C语言实现)

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

本道题目要求是解救007,007被困在一座岛上,周围都是鳄鱼,如果想要解救007,那么我们需要让007跳到对应的鳄鱼头上,直到跳到岸边。第一次跳的时候我们需要判断跳哪一个鳄鱼,是否能跳的上去。题目要求007是在孤岛中心,对应坐标(0,0),当007跳到距离孤岛中心50的时候,就到岸了,孤岛半径是7.5,所以第一跳是7.5加上你给007的跳跃距离。如果大于50-7.5,也就是大于43,那么007可以直接跳到岸上。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 100//鳄鱼最大数
struct Node//x,y坐标
{
	int x;
	int y;
};
typedef struct Node PtrNode;
int N=0;
float step=0;//步长
int s[MAX];//访问数组

我们需要实现的函数

int Visit(PtrNode V);
int DFS(int v, PtrNode *G);//这道题目深度优先遍历更好一点
double distance(PtrNode V,PtrNode W);//计算距离
int FirstJump(PtrNode V);//第一跳
void save007(PtrNode *crocodile,int N);

我们先来看主函数入口

int main()
{
	int i;
	for(i=0;i<MAX;i++)
		s[i]=0;
	scanf("%d %f",&N,&step);
	if(step>=43)//一下子就跳到岸上了
	{
		printf("Yes\n");
		return 0;
	}
	PtrNode crocodile[N];
	for(i=0;i<N;i++)
		scanf("%d %d",&crocodile[i].x,&crocodile[i].y);
	save007(crocodile,N);
	return 0;
}

解救007函数入口

void save007(PtrNode *crocodile,int N)
{
	int i,answer=0;
	for(i=0;i<N;i++)
	{
		if(!s[i] && Firstjump(crocodile[i]))//这个鳄鱼没有跳过而且可以跳上去
		{
			s[i]=1;//这个鳄鱼跳过了
			answer=DFS(i,crocodile);//深度遍历这个鳄鱼
			if(answer==1)//可以到岸
				break;
		}
	}
	if(answer==1)
		printf("Yes\n");
	else
		printf("No\n");
}

判断第一跳

int Firstjump(PtrNode v)
{
	PtrNode center;
	center.x=center.y=0;
	if(distance(center,v)<step+7.5)
		return 1;
	else
		return 0;
}

计算距离

double distance(PtrNode e ,PtrNode v)
{
	return sqrt(pow(e.x-v.x,2)+pow(e.y-v.y,2));
}

深度优先遍历

int DFS(int v,PtrNode *G)
{
	int i,answer=0;
	s[v]=1;//这个鳄鱼跳过了
	answer=Visit(G[v]);//判断是否能跳上岸
	if(answer==0)//这个鳄鱼跳不上岸
	{
		for(i=0;i<N;i++)
		{
			if(!s[i] && distance(G[v],G[i])<=step)//找这个鳄鱼附近的鳄鱼,看看能不能跳上去
				answer=DFS(i,G);//可以跳上去,接着遍历
			if(answer==1)
				break;
		}
	}
	return answer;

}

判断是否直接到岸

int Visit(PtrNode V)
{
	if(V.x<=step-50||V.y<=step-50||V.x>=50-step||V.y>=50-step)
		return 1;
	else
		return 0;
}

文章的总代码如下

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 100
struct Node
{
	int x;
	int y;
};
typedef struct Node PtrNode;
int N=0;
float step=0;
int s[MAX];

int Visit(PtrNode V);
int DFS(int v, PtrNode *G);
double distance(PtrNode V,PtrNode W);
int FirstJump(PtrNode V);
void save007(PtrNode *crocodile,int N);

int main()
{
	int i;
	for(i=0;i<MAX;i++)
		s[i]=0;
	scanf("%d %f",&N,&step);
	if(step>=43)
	{
		printf("Yes\n");
		return 0;
	}
	PtrNode crocodile[N];
	for(i=0;i<N;i++)
		scanf("%d %d",&crocodile[i].x,&crocodile[i].y);
	save007(crocodile,N);
	return 0;
}
void save007(PtrNode *crocodile,int N)
{
	int i,answer=0;
	for(i=0;i<N;i++)
	{
		if(!s[i] && Firstjump(crocodile[i]))
		{
			s[i]=1;
			answer=DFS(i,crocodile);
			if(answer==1)
				break;
		}
	}
	if(answer==1)
		printf("Yes\n");
	else
		printf("No\n");
}
int Visit(PtrNode V)
{
	if(V.x<=step-50||V.y<=step-50||V.x>=50-step||V.y>=50-step)
		return 1;
	else
		return 0;
}
int DFS(int v,PtrNode *G)
{
	int i,answer=0;
	s[v]=1;
	answer=Visit(G[v]);
	if(answer==0)
	{
		for(i=0;i<N;i++)
		{
			if(!s[i] && distance(G[v],G[i])<=step)
				answer=DFS(i,G);
			if(answer==1)
				break;
		}
	}
	return answer;

}
int Firstjump(PtrNode v)
{
	PtrNode center;
	center.x=center.y=0;
	if(distance(center,v)<step+7.5)
		return 1;
	else
		return 0;
}
double distance(PtrNode e ,PtrNode v)
{
	return sqrt(pow(e.x-v.x,2)+pow(e.y-v.y,2));
}

希望大家好好分析步骤,形成一个框架,不要抄,先理解,然后取得进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值