07-图5 Saving James Bond - Hard Version

61 阅读5分钟

Problem Description

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

Solution

题意理解:在 06-图2 Saving James Bond - Easy Version 的基础上,如果不能逃脱输出 0 ,如果可以逃脱输出跳跃次数与路径上的鳄鱼坐标。

使用 Dijkstra 算法求有权无向图的单源最短路径。

注意题中要求当最短路径不止一条(是跳跃次数相等而不是走过的路程相等)时输出第一跳最近的路径。

应对这个条件的方法这里用的是给第一步的权重赋为第一只鳄鱼到湖心的距离,其他结点之间如果可以连通就赋值为 5000 (保证比第一跳权重大,这样在搜索的时候就可以做到只看第一跳的距离,后面的跳跃距离均忽略)。

读入时鳄鱼结点存在结构体数组内,下标为鳄鱼编号 1 ~ N ,0 为湖心岛 ,N + 1 为岸上。

要使用 Dijkstra 算法,需要将图初始化为对角线为 0 ,其他结点间距为无穷大。

算法步骤:

  • 传入参数是源点
  • 需要 dist[] 来存各点到源点的距离,源点到自身的距离初始化为 1 ,其余点到源点的距离初始化为无穷大。
  • 需要 path[] 来存各结点上一个结点,将 path[] 从终点回溯各个结点压入栈,再将栈内所有结点弹出即为最短路径
  • 需要 collected[] 来标记结点是否收入已求出最短路径的结点集合
  • 进入循环,退出条件在循环内
    • 从未收入已求出最短路径结点集合中找出目前 dist 最小的那个结点记为 v
    • 如果不存在这样的结点(所有结点都已标记)就退出循环
    • 将 v 标记为收入已求出最短路径结点集合
    • 遍历 v 未收入已求出最短路径结点集合的邻接点,如果经过 v 能使该邻接点的 dist[i] 减小,就更新 dist[i] = dist[v] + G[v][i]
      • 标记路径 path[i] = v;

一些细节的理解,在课程中也讲了:

  • 每一次找到 v ,这个 v 都会被标记为收入已求出最短路径结点的集合,因为它们是按非递减顺序生成的,保证前面集合内的每一个结点都已经找到最短路径,集合内下一个没有被收入的邻接点就有可能被更新为最短路径,这个最短路径一定是集合内结点的邻接点
  • 每一次遍历都是在不断缩小各个 dist 的距离,一旦遍历结束循环开头从当前未收入的结点里找出 dist 最小的,这个结点就已经找到了最短路径
  • 核心思想:动态规划

做题时遇到的 bug :

  • 在入栈出栈输出最短路径时注意循环退出条件是 i != -1 ,一开始写成了 path[i] != -1 退出早了,没有把路径完全生成完。
  • 输出的时候栈内元素存的是鳄鱼序号,输出的时候应该把栈内元素作为下标而不是栈顶指针。

核心算法都没写错,就是输出最短路径有问题,导致 debug 很久。但认真单步调试跟着 Dijkstra 算法走一遍印象更深。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define INF 600000

typedef struct CNode Crocodile;
struct CNode {
    int x;
    int y;
};

void Input(Crocodile C[], int *N, int *D)
{
    int i, a, b;
    scanf("%d%d", &(*N), &(*D));
    for (i = 1; i <= *N; i++) {
        scanf("%d%d", &a, &b);
        C[i].x = a;
        C[i].y = b;
    }
}

bool FirstJump(Crocodile C[], int i, int D)
{
    int distance2;
    distance2 = C[i].x * C[i].x + C[i].y * C[i].y;
    if (distance2 * 100 <= (D * 10 + 75) * (D * 10 + 75)) return true;
    else return false;
}

bool IsSafe(Crocodile C[], int i, int D)
{
    if (C[i].x + D >= 50 || C[i].x - D <= -50 || C[i].y + D >= 50 || C[i].y - D <= -50) return true;
    else return false;
}

bool Jump(Crocodile C[], int i, int j, int D)
{
    int distance2;
    distance2 = (C[i].x - C[j].x) * (C[i].x - C[j].x) + (C[i].y - C[j].y) * (C[i].y - C[j].y);
    if (distance2 <= D * D) return true;
    else return false;
}

int FirstDistance(Crocodile C[], int i)
{
    return C[i].x * C[i].x + C[i].y * C[i].y;
}

void BuildGraph(Crocodile C[], int G[][102], int N, int D)
{
    int i, j;
    for (i = 0; i <= N + 1; i++) {
        for (j = 0; j <= N + 1; j++) {
            if (i == j) G[i][j] = 0;
            else G[i][j] = G[j][i] = INF;
        }
    }
    if (75 + D * 10 >= 500) {
        G[0][N + 1] = G[N + 1][0] = 5000;
        return;
    }
    for (i = 1; i <= N; i++) {
        if (FirstJump(C, i, D)) G[0][i] = G[i][0] = FirstDistance(C, i);
        if (IsSafe(C, i, D)) G[i][N + 1] = G[N + 1][i] = 5000;
        for (j = 1; j <= N; j++) {
            if (Jump(C, i, j, D)) G[i][j] = G[j][i] = 5000;
        }
    }
}

int MinDistV(int dist[], int collected[], int N)
{
    int i, min = 0;
    for (i = 0; i <= N + 1 && collected[i]; i++) ;
    if (i == N + 2) return -1;
    min = i++;
    for (; i <= N + 1; i++) {
        if (!collected[i] && dist[i] < dist[min])
            min = i;
    }
    return min;
}

void Search(Crocodile C[], int G[][102], int N)
{
    int i, v, collected[102] = {0}, dist[102], path[102];
    for (i = 0; i <= N + 1; i++) {
        dist[i] = INF;
        path[i] = -1;
    }
    dist[0] = 0;
    while (1) {
        v = MinDistV(dist, collected, N);
        if (v == -1) break;
        collected[v] = 1;
        for (i = 0; i <= N + 1; i++) {
            if (!collected[i] && G[i][v] < INF) {
                if (dist[v] + G[v][i] < dist[i]) {
                    dist[i] = dist[v] + G[v][i];
                    path[i] = v;
                }
            }
        }
    }

    if (path[N + 1] == -1) {
        printf("0");
        return;
    }

    int top, stack[102];
    top = 0;
    for (i = N + 1; i != -1; i = path[i])
        stack[top++] = i;
    
    printf("%d\n", top - 1);
    top -= 2;
    while (top > 0) {
        printf("%d %d\n", C[stack[top]].x, C[stack[top]].y);
        top--;
    }
    return;
}

int main()
{
    int i, j, N, D;
    int G[102][102] = {0};
    Crocodile C[102];
    
    Input(C, &N, &D);
    BuildGraph(C, G, N, D);
    Search(C, G, N);

    return 0;
}