ICode9

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

数据结构 07-图5 Saving James Bond - Hard Version (30 分)

2021-05-23 02:01:18  阅读:194  来源: 互联网

标签:10 12 Saving 07 James 30 50 double path


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 a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.

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, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.

Sample Input 1:

17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10
 

Sample Output 1:

4
0 11
10 21
10 35
 

Sample Input 2:

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

Sample Output 2:

0

 

#include <iostream>
#include <vector>
#include <map>
#include <math.h>
#include <algorithm>
using namespace std;

class vertexNode{
public:
    double x;
    double y;
    vertexNode()=default;
    vertexNode(double x_,double y_):x{x_},y{y_}{};
    double getDistence(double ox,double oy){
        return sqrt(pow(ox-x,2)+pow(oy-y,2));
    }
};
bool compare(vector<vertexNode*> l,vector<vertexNode*> r){
    if(l.size()==r.size()){
        return l[0]->getDistence(0, 0)<r[0]->getDistence(0, 0);
    }else{
        return l.size()<r.size();
    }
};
class adjacencyLinkGraphic{
public:
    vector<vertexNode*> vertexs;
    void build(int n){
        double a,b;
        for(int i=0;i<n;i++){
            scanf("%lf %lf",&a,&b);
            vertexNode* newnode=new vertexNode{a,b};
            if(abs(a)>=50||abs(b)>=50||newnode->getDistence(0, 0)<=7.5){//在岸边或者在岛上都是无效点
                continue;
            }else{
                vertexs.push_back(newnode);
            }
        }
    }
    void DFS(vertexNode* vn,map<vertexNode*, int> &visited,int maxDistence,vector<vertexNode*>&path,vector<vector<vertexNode*>> &paths){
        visited[vn]=1;
        if(abs(vn->x)>=(50-maxDistence)||abs(vn->y)>=(50-maxDistence)){
            paths.push_back(path);
        }
        
        for(auto i=0;i<vertexs.size();i++){
            vertexNode* p=vertexs[i];
            if(!visited[p]&&p->getDistence(vn->x, vn->y)<=maxDistence){
                auto it=find(vertexs.begin(), vertexs.end(), vertexs[i]);
                path.push_back(*it);
                DFS(vertexs[i], visited, maxDistence,path,paths);
                path.pop_back();
            }
        }
    }
    void DFSTraversal(int maxDistence){
        map<vertexNode*, int> visited;
        for(int i=0;i<vertexs.size();i++){
            visited[vertexs[i]]=0;
        }
        vector<vertexNode*> potentialStart;
        
        for(int i=0;i<vertexs.size();i++){
            if(vertexs[i]->getDistence(0, 0)<=maxDistence+7.5){
                potentialStart.push_back(vertexs[i]);
            }
        }
        vector<vector<vertexNode*>> paths;
        for(int i=0;i<potentialStart.size();i++){
            vector<vertexNode*> path;
            map<vertexNode*, int> tempvisited=visited;
            path.push_back(potentialStart[i]);
            DFS(potentialStart[i],tempvisited,maxDistence,path,paths);
        }
        if(paths.size()){
            sort(paths.begin(), paths.end(), compare);
            cout << paths[0].size()+1<<endl;
            for(int i=0;i<paths[0].size();i++){
                cout << paths[0][i]->x<<" "<< paths[0][i]->y<<endl;
            }
        }else{
            cout << 0;
        }
    }
};

int main(){
    int n,d;
    cin >> n >> d;
    if(d>50-7.5){
        cout << "1"<<endl;
        return 0;
    }
    adjacencyLinkGraphic ALG;
    ALG.build(n);
    ALG.DFSTraversal(d);
    return 0;
}

 

标签:10,12,Saving,07,James,30,50,double,path
来源: https://www.cnblogs.com/ichiha/p/14800345.html

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

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

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

ICode9版权所有