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

来源:互联网 发布:神机妙算软件安装包 编辑:程序博客网 时间:2024/05/16 14:25

6-图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 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.

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算法来进行计算,但具体来说,整体代码框架如下图,感谢陈越姥姥的讲解
这里写图片描述

#include<stdio.h>#include<stdlib.h>#include<math.h> #define MAXN 150 //最多150个点 int visited[MAXN] = {0};//建立邻接矩阵 struct port{    int x;    int y;}G[MAXN];//坐标 int NV;//节点数 int distance;//007能跳的距离bool jump(int v1,int v2);//计算007一条能不能成功bool issafe(int v);//从v点能否跳上岸bool DFS(int v);//DFSvoid ListComponentsDFS();//DFS连通集 bool FirstJump(int v);//第一跳的可能void save007();//007 主程序int main(){    //freopen("in.txt","r",stdin);    int i;    int x,y;    scanf("%d %d",&NV,&distance);//输入的是鳄鱼数    for( i=0; i<NV; i++)     {        scanf("%d %d",&x,&y);//读入坐标         G[i].x = x;        G[i].y = y;     }    save007();    //freopen("out.txt","w",stdout);    return 0;} //计算007一条能不能成功,排除第一跳和最后一跳 bool jump(int v1,int v2){    int x,y,d;     bool answer;    x = abs(G[v1].x - G[v2].x);    y = abs(G[v1].y - G[v2].y);    d = pow(x,2) + pow(y,2);//求两点的距离     if( d <= pow(distance,2) )    {        answer = true;    }    else    {        answer = false;    }    return answer;}//从v点能否跳上岸 bool issafe(int v){    int x,y;    bool answer;    x = abs(G[v].x);    y = abs(G[v].y);    if( (x+distance >= 50) || (y+distance >= 50))    {        answer = true;    }    else    {        answer = false;    }    return answer;}//DFSbool DFS(int v){    bool answer = false;    visited[v] = 1;    if(issafe(v))    {        answer = true;    }    else    {        for(int i=0; i<NV; i++)        {            if((!visited[i]) &&(jump(v,i)) )//如果联通这个点且没有访问过             {                answer = DFS(i);            }            if(answer == true)            {                break;            }        }           }    return answer;} void ListComponentsDFS(){    int i;      for(i=0; i<NV; i++)    {        if(!visited[i])        {             DFS(i);        }    }}//第一跳的可能 bool FirstJump(int v){    int x,y;    double d;    bool answer;    x = abs(G[v].x);    y = abs(G[v].y);//用绝对值     d =sqrt(pow(x,2) + pow(y,2));    if( d <= (distance+7.5) )//小岛半径7.5,直径15     {        answer = true;    }    else    {        answer = false;    }    return answer; }//007 主程序void save007(){    bool answer;    for(int v=0; v<NV; v++)    {        if(!visited[v] && FirstJump(v)){            answer = DFS(v);            if(answer == true)            {                break;            }        }    }    if(answer == true)    {        printf("Yes\n");    }    else    {        printf("No\n");    }} 
0 0