数据结构 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

 

 

题目本质是 无权值的单源最短路径 问题

 

带权值的单源最短路径用Dijkstra算法解决

求各点之间的最短路径用Floyd算法

最小生成树要用prim 或kruskal算法

 

解决办法有 BFS 配合 路径数组, 这里的路径数组用于存储某个结点的上一个结点, 用于寻找终点逆向到起点的路径 

如果是求最短路径长度,就配合使用路径长度数组 , 方法完全相同,存的值变成了路径长度

如果要求输出路径长度或者从起点到终点的路径   那路径长度数组和路径数组就是解决问题的关键

 

如果题目是要求最大连通子图 或生成树 ,应该使用DFS

 

 

关键点 1. BFS来寻找最短路径 2. 找到出口后, 如何输出起点到终点的路径

 

1.BFS的起点是小岛(圆心半径7.5)外 maxDistence范围内的坐标 

vector<vertexNode*> potentialStart(0);
for(int i=0;i<vertexs.size();i++){
    if(vertexs[i]->getDistence(0, 0)<=maxDistence+7.5){
        potentialStart.push_back(vertexs[i]);
    }
}

当多条路径时,题目要求输出最短路径中 起点 最小的那个, 即起点距离中心点(0,0)最小的那个

因此在寻找出口时,起点应该按距离从小到大的顺序排序,  按序遍历

bool compare1(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();
    }
}

void sortVertexs(){
    sort(vertexs.begin(), vertexs.end(), compare2);
}

 

 

2.当BFS找到出口时,一定是最短路径  此时需要将起点到终点的路径输出,  这里使用preNode数组存储当前节点的上一个节点, 通过寻找上一个结点的方式来找到之前的路径

起点的上一个节点值为null 同时也是结束循环的条件,   终点值使用当前遍历结点 lastNode来记录,当找到出口时, lastNode就是终点值

设置遍历结点p =lastNode;  以 while(p)为条件 将p推入path数组, p=preNode[p];向前寻找起点,  path中存储的就是从终点到起点的路径 , reverse就可以输出起点到终点的路径

 

                vertexNode* p=lastNode;
                while(p){
                    path.push_back(p);
                    p=preNode[p];
                }

 

核心部分BFS配合路径数组  visited数组用于结点是否被访问过 temp为起点 preNode标记结点的上一个结点  queue存储BFS当前待访问结点的队列

            vector<vertexNode*> queue(0);
            map<vertexNode*,vertexNode*> preNode;
            preNode[temp]=nullptr;//起点的上一个结点是null
            queue.push_back(temp);
            bool flag=false;
            vertexNode* lastNode{nullptr};
            while(queue.size()){//这里开始就是典型的BFS算法, 解题的核心步骤是设置当前结点的前一个结点 ,用于标记路径,便于逆向寻找起点
                vertexNode* Current=queue.front();
                queue.erase(queue.begin());
                for(auto i=0;i<vertexs.size();i++){
                    lastNode=vertexs[i];
                    if(!visited[lastNode]&&lastNode->getDistence(Current->x, Current->y)<=maxDistence){//所有可跳的点入队
                        queue.push_back(lastNode);
                        visited[lastNode]=1;
                        preNode[lastNode]=Current;//将当前结点的pre结点设置为current
                        if(abs(lastNode->x)>=50-maxDistence||abs(lastNode->y)>=50-maxDistence){
                            flag=true;
                            break;
                        }
                    }
                }
                if(flag)break;
            }

 

 

 

#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 compare1(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();
    }
};
bool compare2(vertexNode* l,vertexNode* r){
    return l->getDistence(0, 0)<r->getDistence(0, 0);
};
class adjacencyLinkGraphic{
public:
    vector<vertexNode*> vertexs;
    void sortVertexs(){
        sort(vertexs.begin(), vertexs.end(), compare2);
    }
    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 BFS(int &maxDistence,vector<vector<vertexNode*>> &paths){
        vector<vertexNode*> potentialStart(0);
        vector<vertexNode*> path(0);
        for(int i=0;i<vertexs.size();i++){
            if(vertexs[i]->getDistence(0, 0)<=maxDistence+7.5){
                potentialStart.push_back(vertexs[i]);
            }
        }
        map<vertexNode*,int> visited;
        for(int i=0;i<vertexs.size();i++){
            visited[vertexs[i]]=0;
        }
        while(!potentialStart.empty()){
            auto temp=potentialStart.front();
            visited[temp]=1;
            vector<vertexNode*> queue(0);
            //记录每个路径上结点的上一个结点
            //当结点可以跳出循环时, 倒叙将路径上的前置结点推入路径数组
            //翻转后就是从起点到终点的路径
            map<vertexNode*,vertexNode*> preNode;
            preNode[temp]=nullptr;//起点的上一个结点是null
            queue.push_back(temp);
            bool flag=false;
            vertexNode* lastNode;
            while(queue.size()){
                vertexNode* Current=queue.front();
                queue.erase(queue.begin());
                for(auto i=0;i<vertexs.size();i++){
                    vertexNode* p=vertexs[i];
                    if(!visited[p]&&p->getDistence(Current->x, Current->y)<=maxDistence){//所有可跳的点入队
                        queue.push_back(vertexs[i]);
                        visited[vertexs[i]]=1;
                        preNode[vertexs[i]]=Current;//将当前结点的pre结点设置为current
                        lastNode=vertexs[i];
                        if(abs(vertexs[i]->x)>=50-maxDistence||abs(vertexs[i]->y)>=50-maxDistence){
                            flag=true;
                            break;
                        }
                    }
                }
                if(flag)break;
            }
            if(flag){
                vertexNode* p=lastNode;
                while(p){
                    path.push_back(p);
                    p=preNode[p];
                }
                reverse(path.begin(), path.end());
                paths.push_back(path);
                break;
            }
            potentialStart.erase(potentialStart.begin());
        }
    }
    
    void BFSTraversal(int maxDistence){
        vector<vector<vertexNode*>> paths;
        BFS(maxDistence,paths);
        if(paths.size()){
            sort(paths.begin(), paths.end(), compare1);
            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.sortVertexs();
    ALG.BFSTraversal(d);
    return 0;
}

 

posted @ 2021-05-23 01:54  keiiha  阅读(125)  评论(0)    收藏  举报