其他分享
首页 > 其他分享> > 数据结构 06-图2 Saving James Bond - Easy Version (25 分)

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

作者:互联网

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