数据结构基础 — Saving James Bond - Hard Version

这道题我要记录一下,因为我一开始看错题目了额,题目要求求跳的次数最小,我看成要求跳的长度最小,然后我直接就操起了Dijkstra算法,修改一下相等的时候path的确定。然后我把我这个看错题的答案交上去,竟然只有一个样例不过-  -。
所以我一直觉得我是没错的额,虽然百度了一下没人用这个方法做的呀。
只有一个不过,自己实在想不出算法哪里有问题,就找同学帮我看了看,原来是我读错题目额,然后就把这道题的图原本储存的长度全部变为1,就可以啦~~ 啦啦啦~我觉得自己也算个人才了。我感觉只有我一个人会用Dijkstra算这道题了= = 
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
//
//  main.cpp
//  Saving James Bond2
//
//  Created by air on 2018/4/20.
//  Copyright © 2018年 air. All rights reserved.
/*错误测试点:
 最大N,sample1的复杂版,可选路径8条,后面要更新前面的最短路
 在第98行:
 我的基本思路是:如果路径的长度是一样的,那么就是path要不要改的问题了额,唔0.0
             我觉得我的 Dijastra是没有问题的
 
 */
#define Infinity 1<<30
#define NotVertex -1
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;

void init();
void check(int i, int posx, int posy, int max);
void Find(int begin, int N);
void Dijkstra(int begin, int N);
void FirstMinimun(int V, int W);
void test(int N);
void test2(int N);
void test3(int N);

double graph[102][102];
int pos[102][2];
int visit[102];
double dis[102];
int path[102];  //store the path

int main() {
    int V, max;
    cin >> V >> max;
    init();
    for (int i = 2; i < V + 2; i++) {
        cin >> pos[i][0] >> pos[i][1];
        check(i, pos[i][0], pos[i][1], max);
    }
    
    //一步跳到
    if (max >= 42.5) {
        printf("1\n");   //一步跳到的话,应该这样输出的吧
        return 0;
    }
    
    Find(1, V + 2);
    return 0;
}

void FirstMinimun(int V, int W){
    int i, pre1, pre2;
    
    i = V; pre1 = V;
    while(path[i] != 1){
        pre1 = path[i];
        i = pre1;
    }
    
    i = W; pre2 = W;
    while(path[i] != 1){
        pre2 = path[i];
        i = pre2;
    }
    
    if(pow(pos[pre1][0],2) + pow(pos[pre1][1], 2) < pow(pos[pre2][0],2) + pow(pos[pre2][1], 2))
        path[W] = V;
}

//原本的DFS应该改成Dijastra算法
void Dijkstra(int begin, int N) {
    int W;
    int i, min;
    
    dis[begin] = 0;
    
    for(; ;){
        int V = NotVertex;
        min = Infinity;
        for(i = 0; i < N; i++){
            if(!visit[i] && dis[i] < min){
                min = dis[i];
                V = i;
            }
        }                                        //找到最小的那个未被访问过的dis
   
        if(min == Infinity)                      //所有的点都被访问过了
            break;
        
        visit[V] = 1;
        for(W = 0; W < N; W++){
            if(!visit[W]){
                if( dis[W] != Infinity && dis[V] + graph[V][W] == dis[W]){
                    FirstMinimun(V, W);         //就是这里啊 - -
                }
                if(dis[V] + graph[V][W] < dis[W]){
                    dis[W] = dis[V] + graph[V][W];
                    path[W] = V;
                }
            }
        }
    
//        cout << "After Vertex " << V << " "<< "is visited :";
//        test2(N);
//        test3(N);
    }
}

void Find(int begin, int N) {
    Dijkstra(begin, N);
    int number = 1;
    if (dis[0] == Infinity) //那这里应该是return min, if min =  max;则无法到达
        cout << 0 << endl;
    else{
        int tmp = path[0]; //end = 0
        stack<int> S;
        while(path[tmp] != NotVertex){
            S.push(tmp);
            tmp = path[tmp];
            number++;
        }
        cout << number << endl;
        while (!S.empty()) {
            int top = S.top();
            S.pop();
            cout << pos[top][0] << " " << pos[top][1] << endl;
        }
    }
    return;
}


//图的初始化
void init() {
    for (int i = 0; i < 102; i++) {
        for (int j = 0; j < 102; j++) {
            graph[i][j] = Infinity;
        }
        pos[i][0] = 0;
        pos[i][1] = 0;
        visit[i] = 0;
        dis[i] = Infinity;
        path[i] = NotVertex;
    }
    return;
}

//把图画出来,不过也可以不画,pre-set the center as Vertex 1, and the land is Vertex 0;
void check(int V, int posx, int posy, int max) {
    double length;
    if(posx >= 50 || posx <= -50 || posy >= 50 || posy <= -50)   //鳄鱼在岸上
        return;
    if(sqrt(pow(posx, 2) + pow(posy, 2)) - 7.5 <= 0)            //鳄鱼在岛上
        return;
    
    for (int i = 2; i < V; i++) {
        length = sqrt(pow(posx - pos[i][0], 2) + pow(posy - pos[i][1], 2));
        if (length <= max) {
            graph[i][V] = 1;
            graph[V][i] = 1;
        }
    }
    
    //consider the land case
    if (((50 - posx) <= max) || ((posx + 50) <= max) || ((50 - posy) <= max) || ((posy + 50) <= max))
        graph[0][V] = graph[V][0] = 1;
    
    //consider the center island
    if ((7.5 + max)*(7.5 + max) >= posx * posx + posy * posy)
        graph[1][V] = graph[V][1] = 1;
    return;
}


//写测试函数看下过程变量
void test(int N){
    for(int i = 0; i <N; i++){
        for(int j = 0; j < N; j++){
            if(graph[i][j] == Infinity)
                cout << 0 << " ";
            else
                printf("%.1f ",graph[i][j]);
        }
        cout << endl;
    }
}

void test2(int N){
    for(int i = 0; i < N ; i++){
        if(dis[i] == Infinity)
            cout << 0 << " ";
        else
             printf("%.1f ", dis[i]);
    }
    cout << endl;
}

void test3(int N){
    for(int i = 0; i < N ; i++){
        cout << "path " << i << " : " << path[i] << endl;
    }
    cout << endl;
}



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值