07-图5 Saving James Bond - Hard Version (30 分)

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

Code:

#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof a)
#define INF 0x3f3f3f3f
using namespace std;

const int maxn = 320; // 100 + 100*2(边界点:x,y分别一个)+ 1(原点)
struct node
{
    int x, y;
} nds[maxn];
int n, len;
double D;
int vis[maxn], dis[maxn], pre[maxn], fst[maxn];
int g[maxn][maxn];

void init()
{
    mem(g, INF), mem(vis, 0), mem(dis, INF), mem(pre, -1), mem(fst, INF);
}

double fdis(node a, node b)
{
    return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}

void dijkstra(int s)
{
    dis[s] = 0;
    while (1)
    {
        int mi = INF;
        s = -1;
        for (int i = 0; i < len; i++)
            if (!vis[i] && mi > dis[i])
                mi = dis[i], s = i;

        if (s == -1)
            return;
        vis[s] = 1;
        for (int i = 0; i < len; i++)
            if (!vis[i] && g[s][i] == 1)
                if (mi + g[s][i] < dis[i])
                {
                    dis[i] = mi + g[s][i], pre[i] = s;
                    if (s != 0)
                        fst[i] = fst[s]; // 传递第一跳的距离
                }
                else if (mi + g[s][i] == dis[i] && fst[i] > fst[s]) // 路径距离相同时,较第一跳距离比较短的优先
                    pre[i] = s, fst[i] = fst[s];
    }
}

int main()
{
    scanf("%d%lf", &n, &D);
    n++; // 原点也算为一个点
    nds[0].y = nds[0].x = 0;
    for (int i = 1; i < n; i++)
        scanf("%d%d", &nds[i].x, &nds[i].y);
    if (D + 7.5 >= 50) // 特判
    {
        puts("1");
        return 0;
    }

    len = n;
    //加入边界点之后就等于增加了目的地
    for (int i = 1; i < n; i++) // 加入符合D的边界(河岸)的点
        if (50 - abs(nds[i].x) <= D)
        {
            nds[len].x = nds[i].x >= 0 ? 50 : -50;
            nds[len++].y = nds[i].y;
        }
        else if (50 - abs(nds[i].y) <= D)
        {
            nds[len].y = nds[i].y >= 0 ? 50 : -50;
            nds[len++].x = nds[i].x;
        }

    init();
    double diff;
    for (int i = 1; i < len; i++)
    {
        if ((diff = fdis(nds[0], nds[i]) - 7.5) <= D) // 筛选原点到第一跳的距离
            g[i][0] = g[0][i] = 1, fst[i] = diff;     // 算出第一跳的距离

        // 除了原点,第二跳开始各自符合D的建边
        // why 原点到边界的不用算? 因为上面有了特判
        for (int j = 1; j < len; j++)
            if (i != j && fdis(nds[i], nds[j]) <= D)
                g[i][j] = g[j][i] = 1;
    }

    dijkstra(0);

    int mi = INF, h = -1;
    for (int i = n; i < len; i++) // 筛选第一优先级最小dis,第二优先级最小fst
        if (dis[i] < mi || mi == dis[i] && h != -1 && fst[h] > fst[i])
            mi = dis[i], h = i;

    if (mi != INF)
    {
        printf("%d\n", mi);
        vector<int> vec;
        while (h != -1)
        {
            vec.push_back(h);
            h = pre[h];
        }
        for (int i = vec.size() - 2; i > 0; i--) // 原点和边界点无需输出
            printf("%d %d\n", nds[vec[i]].x, nds[vec[i]].y);
    }
    else
        puts("0");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追寻远方的人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值