06-图2 Saving James Bond - Easy Version

来源:互联网 发布:vr眼镜淘宝购物视频 编辑:程序博客网 时间:2024/05/07 07:31

都说了是easy version了…主题是一个DFS,重点是看什么时候跳出。顺便说一句,这个题目中的图,由于点和点之间的边是在经历一定判断之后才建立的,所以可以直接用一个数组把这些点存了,不用在意当下他们的关系。后面根据给出条件看这个点是不是能够和另一个点连起来。


#include <stdio.h>#include <stdlib.h>#include <math.h>#define R 15typedef struct _position{int x;int y;}Position;int Number,Jump;int *Visit;Position *Croco;int find_next(int i,int range);int find_shore(int i,int range);void Save();int main (){scanf("%d %d",&Number,&Jump);Visit=(int*)malloc((Number+1)*sizeof(int));Croco=(Position*)malloc((Number+1)*sizeof(Position));int k,i,j;Croco[0].x=0;Croco[0].y=0;for(k=1;k<Number+1;k++){scanf("%d %d",&i,&j);Croco[k].x=i;Croco[k].y=j;}if(find_shore(0,R+Jump)){printf("Yes");}else{if(Number==0){printf("No");}else{Save();}}return 0;}void Save(){int k=1;int i,j;int flag=0;for(k=1;k<Number+1;k++){i=Croco[k].x;j=Croco[k].y;if(Visit[k]==0 && pow(i,2)+pow(j,2)<=pow(Jump+R,2)){if(find_next(k,Jump)){flag=1;break;}} }if(flag){printf("Yes");}else{printf("No");}}int find_next(int i,int range){ //这个就是那个DFSint x0=Croco[i].x;int y0=Croco[i].y;Visit[i]=1;int x,y,k;int flag=0;if(find_shore(i,range)){flag=1;}else{for(k=1;k<Number+1;k++){x=Croco[k].x;y=Croco[k].y;if(Visit[k]==0 && pow(x-x0,2)+pow(y-y0,2) <= pow(range,2)){Visit[k]=1;flag=find_next(k,range);if(flag){break;}}}}return flag;}int find_shore(int i,int range){int x=Croco[i].x;int y=Croco[i].y;if(x>=0){x=x+range;}else{x=x-range;}if(y>=0){y=y+range;}else{y=y-range;}if(abs(y)>=50 || abs(x)>=50){return 1;}else{return 0;}}




06-图2 Saving James Bond - Easy Version   (25分)

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 2025 -15-25 288 4929 15-35 -25 2827 -29-8 -28-20 -35-25 -20-13 29-30 15-35 4012 12

Sample Output 1:

Yes

Sample Input 2:

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

Sample Output 2:

No
0 0
原创粉丝点击