ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据结构 06-图2 Saving James Bond - Easy Version (25 分)

2021-05-21 21:33:30  阅读:195  来源: 互联网

标签:25 12 Saving int James vn vertexNode data


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

 

 

#include <iostream>
#include <vector>
#include <map>
#include <math.h>
using namespace std;
class vertexNode{//顶点
public:
    vector<int> data;
    vertexNode()=default;
    vertexNode(int x,int y):data{vector<int>{x,y}}{};
};
class AdjacencyListGraphic{//用邻接表 存储 稀疏图
public:
    vector<vertexNode*> vertexs;//邻接表顶点列表
    int vertexNum,arcNum;
    AdjacencyListGraphic()=default;
    bool DFS(vertexNode* vn,map<vertexNode*,int> &visited,int maxdistence){
        if((abs(vn->data[0])>=(50-maxdistence))||(abs(vn->data[1])>=(50-maxdistence)))
        {
            return true;
        }
        bool flag=false;
        for(int i=0;i<vertexs.size()&&!flag;i++){//没有找到出口时继续循环
            if(!visited[vertexs[i]]){
                double dist= sqrt(
                               pow((vertexs[i]->data[0]-vn->data[0]),2)
                               +pow((vertexs[i]->data[1]-vn->data[1]),2)
                               );
                if(dist<=maxdistence){
                    visited[vertexs[i]]=1;
                    flag=DFS(vertexs[i], visited, maxdistence);
                    if(flag)break;
                }
            }
        }
        return flag;
    }
    void canEscape(int maxDistence){
        bool flag=false;
        for(int i=0;i<vertexs.size();i++){
            int dist= sqrt(pow(abs(vertexs[i]->data[0]),2)+pow(abs(vertexs[i]->data[1]),2)); //最大距离为半径内的点都可以是起点
            //起点小岛的范围是圆心半径7.5 因此起步可跳点应该是 跳点距离圆心dist-7.5 或者 最大可跳距离maxDistence+7.5
            if(dist<=(maxDistence+7.5)){
                map<vertexNode*,int> visited;
                for(int j=0;j<vertexs.size();j++){
                    visited[vertexs[j]]=0;
                }
                visited[vertexs[i]]=1;
                flag=DFS(vertexs[i], visited,maxDistence);
                if(flag)break;
            }
        }
        if(flag){
            cout << "Yes"<< endl;;
        }else{
            cout << "No"<<endl;
        }
       
    }
    void build(int n,int maxdistence){
        int x,y;
        for(int i=0;i<n;i++){
            cin >> x >> y;
            vertexNode* vn=new vertexNode(x,y);
            vertexs.push_back(vn);
        }
    }
};
int main(){
    int n,maxDistence;
    cin >> n >> maxDistence;
    AdjacencyListGraphic ALG;
    ALG.build(n,maxDistence);
    ALG.canEscape(maxDistence);
    return 0;
}

 

标签:25,12,Saving,int,James,vn,vertexNode,data
来源: https://www.cnblogs.com/ichiha/p/14797437.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有