C语言 06-图2 Saving James Bond - Easy Version

本文讨论了电影《生死关头》中的一幕,詹姆斯·邦德如何利用大胆的策略从鳄鱼湖中逃脱。通过分析湖面、岛屿和鳄鱼的位置,确定邦德的最大跳跃距离,来判断他是否能成功逃离。问题转化为在给定的坐标中,利用深度优先搜索(DFS)策略,找出从中央岛屿到湖岸的安全路径。虽然题目涉及图论,但实际解题过程中无需构建图。文章强调了DFS递归逻辑的重要性,并提到了在实现过程中的一个调试经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Show me the Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MaxSize 100
struct Node
{
    int X;
    int Y;
};
typedef struct Node PtrToNode;
float STEP = 0;
int N = 0;
int Visited[MaxSize];
int Visit(PtrToNode V);
int DFS(int v, PtrToNode *G);
double distance(PtrToNode V,PtrToNode W);
int FirstJump(PtrToNode V);
void save007(PtrToNode *crocodile,int N);

int main()
{
    int i;
    for (i = 0;i < MaxSize; i++)
        Visited[i] = 0;

    scanf("%d %f", &N,&STEP);
    if (STEP >= 43)
    {
        printf("Yes\n");
        return 0;
    }
    PtrToNode crocodile[N];
    for(i = 0;i < N;i++)
    {
        scanf("%d %d",&crocodile[i].X, &crocodile[i].Y);
    }
    save007(crocodile,N);
    return 0;
}
void save007(PtrToNode *crocodile,int N)
{
    int i,answer = 0;
    for (i = 0;i < N;i++)
    {
        if(!Visited[i] && FirstJump(crocodile[i]))
        {
            Visited[i] = 1;
            answer = DFS(i,crocodile);
            if (answer == 1)
                break;
        }
    }
    if (answer == 1)
        printf("Yes\n");
    else printf("No\n");
}

int FirstJump(PtrToNode V)
{
    PtrToNode center;
    center.X = 0;
    center.Y = 0;
    int a = distance(center,V);
    if(distance(center,V) < STEP+7.5){
        return 1;
    }
    else
        return 0;
}
double distance(PtrToNode V,PtrToNode W)
{
    return sqrt(pow(V.X - W.X,2)+pow(V.Y-W.Y,2));
}
int DFS(int v, PtrToNode *G)
{
    int i, answer = 0;
    Visited[v] = 1;
    answer = Visit(G[v]);
    if(answer == 0)
    {
        for(i = 0; i < N; i++ )
        {
            if(!Visited[i] && distance(G[v],G[i]) <= STEP)
                answer = DFS(i,G);
            if (answer == 1)
                break;
        }
    }
    return answer;
}

int Visit(PtrToNode V)
{
    if (V.X <= STEP-50 || V.Y <= STEP-50 || V.X >= 50-STEP || V.Y >= 50-STEP)
        return 1;
    else
        return 0;
}

结果:

这里写图片描述

思路与问题:

本题虽然打着图的标签,但是事实上不需要建图就可以做出来,用的依然是深度优先搜索的思想,通过递归去实现对下一跳可行解的查找。注意的问题是,在题中,第一跳和其他跳有所不同,岸边也是图的节点之一。
在DFS中,递归逻辑要清晰,自己在实现的过程中出现了扫描的过程中误加了(未访问)的条件,导致一个不太显眼的bug,最终放到了IDE上进行调试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值