图5 Saving James Bond - Hard Version

文章描述了一个基于电影《LiveandLetDie》的情境,詹姆斯·邦德需从鳄鱼湖中央的小岛上逃脱。问题转化为计算给定跳跃距离和鳄鱼位置时,邦德到达湖岸的最短路径。输入包含鳄鱼的位置和最大跳跃距离,输出是最短路径的跳跃次数及路径。文章涉及图形算法和最短路径计算,解决此类问题需要考虑边节点、顶点和图的构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

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:

<span style="color:#404040"><code class="language-in">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
</code></span>

Sample Output 1:

<span style="color:#404040"><code class="language-out">4
0 11
10 21
10 35
</code></span>

Sample Input 2:

<span style="color:#404040"><code>4 13
-12 12
12 12
-12 -12
12 -12
</code></span>

Sample Output 2:

<span style="background-color:var(--bg-base)"><span style="color:#404040"><code>0</code></span></span>

解答 

#include <iostream>
#include <queue>
#include <vector>

const int INFINITY = 65535;

bool validJump(const std::pair<int, int> &a, const std::pair<int, int> &b, double radius)
{
    return (((a.first - b.first) * (a.first - b.first) + \
                (a.second - b.second) * (a.second - b.second)) <= \
                (radius * radius));
}

double length(const std::pair<int, int> &a)
{
    return (a.first*a.first + a.second*a.second);
}

bool isSafe(const std::pair<int, int> &a, double radius)
{
    return ((50 - a.first <= radius) or \
        (50 + a.first <= radius) or \
        (50 - a.second <= radius) or \
        (50 + a.second <= radius));
}

void getRoute(int *path, int i, std::vector<int> &route)
{
    route.push_back(i);
    i = path[i];
    if (i == -1)
        return;
    getRoute(path, i, route);
}

//边节点
struct ENode
{
    int V1, V2;
    int Weight;

    ENode(int u, int v) : V1(u), V2(v), Weight(1) {}

    ENode(int u, int v, int w) : V1(u), V2(v), Weight(w) {}
};

//边终点
struct ANode
{
    //边终点下标
    int Index;
    int Weight;
    ANode *Next;

    ANode() : Index(0), Weight(INFINITY), Next(nullptr) {}

    ANode(int index, int weight, ANode *next) : Index(index), Weight(weight), Next(next) {}
};

//图顶点
struct VNode
{
    std::pair<int, int> Data;
    //首插法第一条边
    ANode *FirstEdge;

    VNode() : Data(std::make_pair(0, 0)), FirstEdge(nullptr) {}
};

class Graph
{
public:
    int Nv;
    double Radius;
    VNode *G;

    Graph(int n, double r) : Nv(0), Radius(r)
    {
        G = new VNode[n];
        std::pair<int, int> point;
        for (int i = 0; i < n; ++i)
        {
            std::cin >> point.first >> point.second;
            if (point.first * point.first + point.second * point.second <= 56.25)
                continue;
            G[i].Data = point;
            ++Nv;
            for (int j = 0; j < i; ++j)
            {
                if (validJump(G[i].Data, G[j].Data, r))
                    insert(ENode{i, j, 1});
            }
        }
    }

    void insert(const ENode &E) const
    {
        auto *p = new ANode(E.V2, E.Weight, G[E.V1].FirstEdge);
        G[E.V1].FirstEdge = p;
        p = new ANode(E.V1, E.Weight, G[E.V2].FirstEdge);
        G[E.V2].FirstEdge = p;
    }

    void print() const
    {
        std::cout << "*************\n";
        for (int V = 0; V < Nv; ++V)
        {
            for (auto U = G[V].FirstEdge; U; U = U->Next)
            {
                std::cout << V << '-' << '-' << U->Index << ':' << U->Weight << '\n';
            }
            std::cout << "*************\n";
        }
        std::cout << '\n';
    }

    ~Graph()
    {
        delete[]G;
    }
};

//dist全部初始化为INFINITY,path全部初始化为-1
void minDist(const Graph &g, int *dist, int *path, int S = 0)
{
    std::queue<int> Q;
    dist[S] = 0;
    Q.push(S);
    while (!Q.empty())
    {
        int V = Q.front();
        Q.pop();
        for (auto U = g.G[V].FirstEdge; U; U = U->Next)
            if (dist[U->Index] > dist[V] + U->Weight)
            {
                dist[U->Index] = dist[V] + U->Weight;
                path[U->Index] = V;
                Q.push(U->Index);
            }
    }
}

int main()
{
    int N;
    double D;
    std::cin >> N >> D;
    if (isSafe(std::make_pair(0, 0), D + 7.5))
    {
        std::cout << '1' << '\n';
        return 0;
    }
    int countJump = INFINITY;
    int final;
    int index = -1;
    std::vector<int> Destination;
    std::vector<std::vector<int>> answers;
    Graph g(N, D);
    N = g.Nv;
    for (int i = 0; i < N; ++i)
    {
        if (isSafe(g.G[i].Data, D))
            Destination.push_back(i);
    }
    int *dist = new int[N];
    int *path = new int[N];
    for (int k = 0; k < N; ++k)
    {
        if (validJump(std::make_pair(0, 0), g.G[k].Data, 7.5 + D))
        {
            for (int i = 0; i < N; ++i)
            {
                dist[i] = INFINITY;
                path[i] = -1;
            }
            minDist(g, dist, path, k);
//            countJump = INFINITY;
            final = -1;
            for (int j: Destination)
            {
                if (countJump >= dist[j])
                {
                    countJump = dist[j];
                    final = j;
                }
            }
            if (final != -1)
            {
//                std::cout << "countJump = " << countJump << ' ' << "final = " << final << '\n';
                std::vector<int> route;
                getRoute(path, final, route);
                answers.push_back(route);
                ++index;
//                for (int i = 0; i < N; ++i)
//                {
//                    std::cout << dist[i] << ' ';
//                }
//                std::cout << '\n';
//                for (int i: route) std::cout << i << ' ';
//                std::cout << '\n';
            }
        }
    }
//    g.print();
    if (index == -1)
        std::cout << '0' << '\n';
    else
    {
        std::cout << countJump + 2 << '\n';
        if (answers[index - 1].size() == answers[index].size())
        {
            int x = answers[index].size() - 1;
            if (length(g.G[answers[index - 1][x]].Data) < length(g.G[answers[index][x]].Data))
                --index;
        }
        for (auto i = answers[index].rbegin(); i != answers[index].rend(); ++i)
            std::cout << g.G[*i].Data.first << ' ' << g.G[*i].Data.second << '\n';
    }
    delete[]dist;
    delete[]path;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值