我是靠谱客的博主 平常发带,最近开发中收集的这篇文章主要介绍07-图5 Saving James Bond - Hard Version (30 分)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("%dn", 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 %dn", nds[vec[i]].x, nds[vec[i]].y);
}
else
puts("0");
return 0;
}

最后

以上就是平常发带为你收集整理的07-图5 Saving James Bond - Hard Version (30 分)07-图5 Saving James Bond - Hard Version (30 分)的全部内容,希望文章能够帮你解决07-图5 Saving James Bond - Hard Version (30 分)07-图5 Saving James Bond - Hard Version (30 分)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部