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

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;
}

 

上一篇:【Codeforces 1443 B】Saving the City,贪心,分类讨论


下一篇:美语初级 L063:Saving a Drowning Boy 解析