我是靠谱客的博主 苗条吐司,最近开发中收集的这篇文章主要介绍07-图5 Saving James Bond - Hard Version,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

07-图5 Saving James Bond - Hard Version

来自:PTA_数据结构_Saving James Bond - Hard Version

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

程序代码:(没看懂)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* 队列定义开始 */
#define MaxSize 101
#define ERROR -1
typedef int Position;
struct QNode {
int *Data;
/* 存储元素的数组 */
Position Front, Rear;
/* 队列的头、尾指针 */
};
typedef struct QNode *Queue;
Queue CreateQueue()
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (int *)malloc(MaxSize * sizeof(int));
Q->Front = Q->Rear = 0;
return Q;
}
void DestoryQueue( Queue Q )
{
if (Q->Data) free(Q->Data);
free(Q);
}
int IsFull( Queue Q )
{
return ((Q->Rear+1)%MaxSize == Q->Front);
}
void Enqueue( Queue Q, int X )
{
if ( IsFull(Q) ) return;
else {
Q->Rear = (Q->Rear+1)%MaxSize;
Q->Data[Q->Rear] = X;
}
}
int IsEmpty( Queue Q )
{
return (Q->Front == Q->Rear);
}
int Dequeue( Queue Q )
{
if ( IsEmpty(Q) ) return ERROR;
else
{
Q->Front =(Q->Front+1)%MaxSize;
return
Q->Data[Q->Front];
}
}
/* 队列定义结束 */
#define MaxVertexNum 100
#define INF 65535
#define ISLAND_DIAMETER 15/2.0
typedef struct Position Point;
struct Position {
int x;
int y;
};
Point points[MaxVertexNum];
int dist[MaxVertexNum];
// 路径长度
int path[MaxVertexNum];
// 下一个结点
void ReadPoints(int num);
int Distance(Point p1, Point p2);
//求两点间距离
int Jump(Point p1, Point p2, int dis);
// 判断能否跳跃过去
int isSafe(Point p, int dis);
// 是否能跳上岸
void PrintPath(int ans);
void Save007(int num, int dis); // 其实就是一个BFS函数
int main()
{
int num, dis;
scanf("%d %d", &num, &dis);
ReadPoints(num);
Save007(num, dis);
return 0;
}
void ReadPoints(int num)
{
int i;
for (i = 0; i < num; ++i) {
scanf("%d %d", &(points[i].x), &(points[i].y));
}
}
int Distance(Point p1, Point p2)
{
return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}
int Jump(Point p1, Point p2, int dis)
{
return Distance(p1, p2) <= dis * dis;
}
int isSafe(Point p, int dis)
{
return ((abs(p.x) + dis) >= 50 || (abs(p.y) + dis) >= 50);
}
void PrintPath(int ans)
{
printf("%dn", dist[ans] + 1);
for (; ans != -1; ans = path[ans]) {
printf("%d %dn", points[ans].x, points[ans].y);
}
}
void Save007(int num, int dis)
{
int i, cur_dis, pIndex, ans;
Queue Q;
Point center;
center.x = 0; center.y = 0;
if (isSafe(center, dis + ISLAND_DIAMETER)) {
// 如果从岛上就能直接跳上岸
printf("1n");
return;
}
for (i = 0; i < num; ++i) {
// 初始化辅助数组
dist[i] = INF;
path[i] = -1;
}
Q = CreateQueue();
for (i = 0; i < num; ++i) {
if (isSafe(points[i], dis)) {
// 将所有能上岸的入队
dist[i] = 1;
Enqueue(Q, i);
}
}
cur_dis = 1; ans = -1;
while (!IsEmpty(Q)) {
pIndex = Dequeue(Q);
if (dist[pIndex] == cur_dis + 1 && ans != -1) break;
// 已经遍历完当前层
if (Jump(center, points[pIndex], dis + ISLAND_DIAMETER)) {
if (ans == -1 || Distance(center, points[pIndex]) < Distance(center, points[ans]))
ans = pIndex;
}
else {
for (i = 0; i < num; ++i) {
if (Jump(points[pIndex], points[i], dis) && (dist[pIndex] + 1 < dist[i])) {
dist[i] = dist[pIndex] + 1;
path[i] = pIndex;
Enqueue(Q, i);
}
}
}
cur_dis = dist[pIndex];
}
if (ans == -1) printf("0n");
else PrintPath(ans);
DestoryQueue(Q);
}

其实我的思路是:
先找出从岛上第一步能够跳到的点,然后分别对这些点进行Dijkstra,记录到每个点的步数。然后扫描所有点,找出能直接跳到岸上且步数最少的点。然后进行回溯。

最后

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

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部