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和鳄鱼)的位置信息保存在图中,然后通过判断点与点之间能否跳过,来进行连接,其中James的位置假设为(0,0),他能活动的范围是直径为15的小岛,所以所有与James连通的点的条件要减去活动的范围,连通之后,通过DFS或BFS遍历找到是否有连通的集合中的点满足到达条件。
到达条件:最后的鳄鱼的x或y<=跳跃距离。

代码:

#include <iostream>
#include <queue>
#include <math.h>

#define MaxVertexNum 101    //最大顶点数设为101,最大鳄鱼数加初始位置

using namespace std;
using Vertex = int;         // 用顶点下标表示顶点,为整型
using WeightType = int;   //定义权重类型为int

using point = struct Node;
struct Node {    //坐标
    int x;
    int y;
};

//图
class Graph {
public:
    int Nv;  // 顶点数
    int Ne;  // 边数 
    int jump;  //跳跃距离
    WeightType G[MaxVertexNum][MaxVertexNum]; // 邻接矩阵 
    point* Data;   //存放点信息
    bool* Visited;  //标志是否访问过
public:
    Graph(int VertexNum, int jump_);  //带参构造函数
    void InsertEdge(Vertex V1, Vertex V2);    //插入边
    void BuildEdge();    //将符合的点连上边
    bool DFS(Vertex V); //DFS
    void Init_Visited();  //将Visited全部变为false
};

int main() {
    int Num_of_cro, jump_dis;   //鳄鱼数,跳跃距离
    int x, y;   //坐标
    
    cin >> Num_of_cro >> jump_dis;
    Graph G{ Num_of_cro + 1, jump_dis};   //创建图

    //读入点的值
    for (int i = 1; i <= Num_of_cro; i++) {
        cin >> x >> y;
        G.Data[i].x = x;
        G.Data[i].y = y;
    }

    G.BuildEdge();   //将符合的点连上边
    G.Init_Visited(); 

    if (G.DFS(0)) cout << "Yes";
    else cout << "No";

    return 0;
}

Graph::Graph(int VertexNum, int jump_) {
    Nv = VertexNum;
    Ne = 0;
    Data = new point[VertexNum];
    Visited = new bool[VertexNum];

    this->jump = jump_;
    for (int i = 0; i < VertexNum; i++) {
        for (int j = 0; j < VertexNum; j++) {
            G[i][j] = 0;
        }
    }
    Data[0].x = 0;
    Data[0].y = 0;
}

void Graph::InsertEdge(Vertex V1, Vertex V2) {
    //这里可以加判断有没有加过这条边

    //插入边 <V1, V2>
    G[V1][V2] = 1;
    //插入边 <V2, V1>
    G[V2][V1] = 1;

    Ne++;//边数加1
}

void Graph::BuildEdge() {
    for (int i = 1; i < Nv; i++) {
        int dis = pow(Data[i].x, 2) + pow(Data[i].y, 2);
        if ((pow(dis, 0.5) - 7.5) <= jump) {
            InsertEdge(0, i);
        }
    }
    for (int i = 1; i < Nv; i++) {
        for (int j = i+1; j < Nv; j++) {
            if ((pow(Data[i].x - Data[j].x, 2) + pow(Data[i].y - Data[j].y, 2)) <= pow(jump, 2)) {
                InsertEdge(i, j);
            }
        }
    }
}

bool Graph::DFS(Vertex V) {
    int flag=0;
    Visited[V] = true; // 标记V已访问,防止出现死循环 

    for (Vertex i = 0; i < Nv; i++) {
        if (!Visited[i] && G[V][i]) {
            if ((pow(50 - abs(Data[i].x), 2) <= pow(jump, 2)) || (pow(50 - abs(Data[i].y), 2) <= pow(jump, 2))) {
                flag=1;
                break;
            }
            if (DFS(i)) {
                flag = 1;
                break;
            }
        }
    }
    if (flag) return true;
    else return false;
}

void Graph::Init_Visited() {
    for (int i = 0; i < Nv; i++) {
        Visited[i] = false;
    }
}

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

积木41

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

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

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

打赏作者

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

抵扣说明:

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

余额充值