06-图2 Saving James Bond - Easy Version (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);//DFS
void 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;
}

//DFS
bool 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值