【图】 Saving James Bond - Easy Version

【图】 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.

输入格式:

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.

输出格式:

For each test case, print in a line “Yes” if James can escape, or “No” if not.

输入样例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

输出样例1:

Yes

输入样例2:

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

输出样例2:

No

解题思路:
  1. 将每个鳄鱼视为图结构的顶点,判断他是否能获救的根本是判断从指定点(中心圆)出发遍历图能否在某个顶点上跳上岸。
  2. 从圆出发进行遍历,使用深度优先搜索,每遍历到一个顶点判断是否能跳上岸。
  3. 从圆出发向别的顶点跳,实际上就是从圆心出发向别的顶点跳,判断跳跃距离是否大于等于R + D。
  4. 从一个顶点向另一个顶点跳,实际上就是判断跳跃距离是否大于等于两点距离。
  5. 本题的实质是:加上限定条件的深度优先搜索。初始条件(从圆心向别的顶点跳)、遍历条件(两点距离小于等于跳跃距离)、结束条件(顶点到岸的距离小于等于跳跃距离)。
  6. 使用机构体数组来存储顶点的位置信息。
注意:

第一次遍历和后续遍历不同,需要分开编写。

完整程序:
/*
【解题思路】
1.将每个鳄鱼视为图结构的顶点,判断他是否能获救的根本是判断从指定点(中心圆)出发遍历图能否在某个顶点上跳上岸
2.从圆出发进行遍历,使用深度优先搜索,每遍历到一个顶点判断是否能跳上岸
3.从圆出发向别的顶点跳,实际上就是从圆心出发向别的顶点跳,判断跳跃距离是否大于等于R + D
4.从一个顶点向另一个顶点跳,实际上就是判断跳跃距离是否大于等于两点距离
5.本题的实质是:加上限定条件的深度优先搜索。初始条件(从圆心向别的顶点跳)、遍历条件(两点距离小于等于跳跃距离)、结束条件(顶点到岸的距离小于等于跳跃距离)
6.使用机构体数组来存储顶点的位置信息
*/

/*【注意】第一次遍历和后续遍历不同,需要分开编写*/
#include <cstdio>
#include <cstdlib>
#include <cmath>

const int MaxSize = 100;
const double R = 15 / 2;

struct Node {
    int x; // 顶点横坐标
    int y; // 顶点纵坐标
    bool visited; // 标记该顶点是否被遍历过
} position[MaxSize]; // 用来存放所有顶点

bool FirstJump(int D,int i); // 第一次跳跃是否成功
bool IsJump(int D,int i,int j); // 两顶点之间的跳跃是否成功
bool IsSave(int D,int i); // 是否可以成功获救(上岸)
bool DFS(int N,int D,int i); // 从序号i的顶点开始深度优先搜索
void Save007(int N,int D); // 综合函数

bool FirstJump(int D,int i) // i:顶点序号
{
    bool isSuccess; // 是否成功
    isSuccess = sqrt(pow(position[i].x,2) + pow(position[i].y,2)) <= R + D;
    return isSuccess;
}

bool IsJump(int D,int i,int j) // i:顶点1的序号;j:顶点2的序号
{
    bool isSuccess; // 是否成功
    isSuccess = sqrt(pow(position[i].x - position[j].x,2) + pow(position[j].x - position[j].x,2)) <= D;
    return isSuccess;
}

bool IsSave(int D,int i)
{
    bool isSuccess; // 是否成功
    isSuccess =  50 - abs(position[i].x) <= D || 50 - abs(position[i].y) <= D;
    return isSuccess;
}

bool DFS(int N,int D,int i)
{
    position[i].visited = true;
    int k;
    if(IsSave(D,i))
        return true;
    else {
        for(k = 0;k < N;k ++) {
            if(!position[k].visited && IsJump(D,i,k)) {
                bool isJump = DFS(N,D,k);
                if(isJump)  return true;
            }
        }
    }
    return false;
}

void Save007(int N,int D)
{
    int k;
    bool isJump = false;
    for(k = 0;k < N;k ++) {
        if(!position[k].visited && FirstJump(D,k)) {
            isJump = DFS(N,D,k);
            if(isJump)  break;
        }
    }
    if(isJump)
        printf("Yes\n");
    else
        printf("No\n");
}

int main()
{
    int N,D; // N:鳄鱼数量;D:跳跃的最大距离
    int i;
    scanf("%d %d",&N,&D);
    for(i = 0;i < N;i ++) {
        scanf("%d %d",&position[i].x,&position[i].y);
        position[i].visited = false;
    }
    Save007(N,D);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百栗.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值