06-图2 Saving James Bond - Easy Version

题目:

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

算法逻辑与分析:
把问题抽象为一个图,将James能否跳出的问题,抽象为一个listComponents问题,即对连通分量遍历,并在满足特定条件时就退出。
图的表达:
顶点:鳄鱼位置的结构数组,每条鳄鱼有一个编号v,对应坐标v(x,y)
边:边不是固定的,所以不生成图
权重:恒定为maxJumpDist

流程【伪代码】:
生成一个图,图只有顶点,没有边。【buildLake】;
对图做listComponents,选择一个顶点,如果满足【firstJump】,并且没有被访问过,则用visited数组跟踪该顶点访问情况;
判断是否到边【canJumpOntoBank】,到边即退出,否则尝试跳跃到其它顶点【canJumpOntoNext】,做dfs,直至结束;
释放申请的空间【free(lake);】。

手绘示意图:
在这里插入图片描述

代码:

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

#define MAX_NUM_CROCODILES 100
#define DISTANCE_TO_BANK 50
#define ISLAND_RADIUS 7.5

typedef int Vertex;

// 顶点(鳄鱼)的坐标
typedef struct _Coordinate
{
    int x, y;
} Coordinate[MAX_NUM_CROCODILES];

// numCrocodiles:鳄鱼的个数;maxJumpDist:最大跳跃距离;Coordinate crocodiles:鳄鱼的坐标
struct _Lake
{
    int numCrocodiles, maxJumpDist;
    Coordinate crocodiles;
};
typedef struct _Lake *Lake;

Lake buildLake();
bool firstJump(Lake lake, Vertex v);
bool canJumpOntoNext(Lake lake, Vertex v, Vertex w);
bool canJumpOntoBank(Lake lake, Vertex v);
bool dfs(Lake lake, Vertex v, bool visited[]);
void save007(Lake lake);
void freeLake(Lake lake);

/*
06-图2 Saving James Bond - Easy Version
难度:2颗星,是DFS的一个具体应用。

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

yes

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

no

 */

int main()
{
    Lake lake = buildLake();
    save007(lake);

    return 0;
}

Lake buildLake()
{
    Lake lake = (Lake)malloc(sizeof(struct _Lake));
    scanf("%d %d", &lake->numCrocodiles, &lake->maxJumpDist);
    Vertex v;
    for (v = 0; v < lake->numCrocodiles; v++)
        scanf("%d %d", &lake->crocodiles[v].x, &lake->crocodiles[v].y);

    return lake;
}

bool firstJump(Lake lake, Vertex v)
{
    float distance = sqrt(pow(lake->crocodiles[v].x, 2) + pow(lake->crocodiles[v].y, 2));
    //浮点数比较需要考虑精度问题
    if (distance - ISLAND_RADIUS <= lake->maxJumpDist)
        return true;
    else
        return false;
}

bool canJumpOntoNext(Lake lake, Vertex v, Vertex w)
{
    float distance = sqrt(pow(lake->crocodiles[v].x - lake->crocodiles[w].x, 2) + pow(lake->crocodiles[v].y - lake->crocodiles[w].y, 2));
    if (distance <= lake->maxJumpDist)
        return true;
    else
        return false;
}

bool canJumpOntoBank(Lake lake, Vertex v)
{
    if (DISTANCE_TO_BANK - abs(lake->crocodiles[v].x) <= lake->maxJumpDist || DISTANCE_TO_BANK - abs(lake->crocodiles[v].y) <= lake->maxJumpDist)
        return true;
    else
        return false;
}

bool dfs(Lake lake, Vertex v, bool visited[])
{
    visited[v] = true;
    bool isSafety = false;
    if (canJumpOntoBank(lake, v))
    {
        isSafety = true;
    }
    else
    {
        Vertex w;
        for (w = 0; w < lake->numCrocodiles; w++)
        {
            if (!visited[w] && canJumpOntoNext(lake, v, w))
                isSafety = dfs(lake, w, visited);
            if (isSafety)
                break;
        }
    }

    return isSafety;
}

// 核心算法
void save007(Lake lake)
{
    // 初始化顶点访问数组,lake->numCrocodiles是一个变量,所以不能直接用简单方式初始化。
    bool visited[lake->numCrocodiles];
    Vertex v;

    for (v = 0; v < lake->numCrocodiles; v++)
    {
        visited[v] = false;
    }

    // 根据isSafety的值确定解救是否成功
    bool isSafety = false;

    // 遍历每个可以满足第一跳的顶点(鳄鱼),再根据该顶点dfs寻找出口,并返回结果
    for (v = 0; v < lake->numCrocodiles; v++)
    {
        if (!visited[v] && firstJump(lake, v))
        {
            isSafety = dfs(lake, v, visited);
            if (isSafety)
            {
                break;
            }
        }
    }

    if (isSafety)
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }

    free(lake);
}


测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值