凛冬之翼---拯救007

花了一个上午写出来的代码,这道题目的背景很有意思,我小时候也看过007的电影中007从鳄鱼池逃跑的片段感觉题目很有意思。今天早点睡觉,明天早一点到实验室。

题目:
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.

假设湖是一个100乘100的正方形。 假设湖的中心在(0,0),东北角在(50,50)。 中央岛是直径为15,以(0,0)为中心的圆盘。许多鳄鱼在湖的不同位置。考虑到每个鳄鱼的坐标和詹姆斯可以跳跃的距离,你必须告诉他他是否可以逃脱。

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

每个输入文件包含一个测试用例。 每个案例从一个包含两个正整数的行开始,鳄鱼的数量N(≤100)和詹姆斯可以跳跃的最大距离D。 接下来的N行,每个包含鳄鱼的(x,y)位置。 注意,没有两个鳄鱼停留在相同的位置。

Output Specification:
For each test case, print in a line “Yes” if James can escape, or “No” if not.

对于每个测试用例,如果James可以逃脱,则在行中输入“是”,否则输出“否”。

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

解题思路:
1.这里我主要使用了DFS的思路来解题,其中有递归的思想,就是当007跳到一个鳄鱼头上时,就把这个鳄鱼头标记为已经访问过,然后再递归访问周围所有可以访问的鳄鱼头,递归到下一个鳄鱼头之前先检查是否可以从这个鳄鱼头跳上岸。
2.数据结构用一个数轴crocodile[Max]来表示,它的结构里面包含了一个坐标的x值和y值。然后用另一个visited数轴表示是否访问过。

扫描二维码关注公众号,回复: 4060549 查看本文章

遇到的问题:
1.其中false和true是用字符还是整形来表示有点头晕,直接在头文件下面定义#define false 0 #define true 1 来得实际。
2.在调用函数时后面的()错误打成了[]花了好多时间找出来,下次要仔细。
3.多些一点注释,因为现在的算法也开始逐渐复杂起来了,不写注解一段时间再看就有点摸不着头脑了。以上。

代码:

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

#define Max 101
#define True 1
#define False 0

struct crocodile{       //the struct define the x and y coordinate of the crocodile
	int x;
	int y;
}crocodile[Max];

int result=False;
int visited[Max]={0};  //using visited to pretend whether the crocodile has been visited 
int N,D;        //maro define the Max number of crocodile and the longest distance

double Distance(int i,int j){    //calculate distance between two crocodiles
	double x;
	x=sqrt(pow(crocodile[i].x-crocodile[j].x,2)+pow(crocodile[i].y-crocodile[j].y,2)); //sqrt (xi-yj)^2-(yi-yj)^2
	return x;
}

int Escape(int i){             //the check whether 007 can jump from crocodile to bank
    if((crocodile[i].x<=D-50)||(crocodile[i].x>=50-D)||(crocodile[i].y<=D-50)||(crocodile[i].y>=50-D))
	return True;
	else
	return False; 
}

int DFS(int i){
	visited[i]=True;
	if(Escape(i)) result=True;    //if the crocodile can escape set yes
	else{
		for(int j=1;j<=N;j++){
		if((!visited[j])&&(Distance(i,j)<=D)){  //if the crocodile is'nt visited and 007 can jump to it 
			result=DFS(j);
		}
		}		
	}
	return result;  	
}



int main(){
    scanf("%d %d",&N,&D);
    visited[0]=True;
    crocodile[0].x=0;
    crocodile[0].y=0;    //first begin in the middle of the (0,0)
    for(int i=1;i<=N;i++){  //get the coordinate of the crocodile
    	scanf("%d %d",&crocodile[i].x,&crocodile[i].y);
	}
    for(int i=1;i<=N;i++){       //from i=1 to N to jump 
    if(Distance(0,i)<=(D+7.5)) {  //whether 007 can jump from (0,0) to one crocodile
    		result=DFS(i);
    		if(result){
    			printf("Yes"); break;
			}
	}	
	}
	
	if(!result) printf("No");
    
}

猜你喜欢

转载自blog.csdn.net/weixin_39042981/article/details/84027453
今日推荐