07-图5 Saving James Bond - Hard Version (30分)(数据结构)(c语言)

2023-11-28 18:48

本文主要是介绍07-图5 Saving James Bond - Hard Version (30分)(数据结构)(c语言),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

这道题目挺难的,我当时做这道题目的时候想了好久,比较复杂。
我们在做这道题目的时候,不仅要考虑007能否上岸,还要考虑007所走的路径,我的大概思路是这样的,首先进行筛选,对不在湖中的和坐标小于岛上的,我们进行过滤掉,也就是不做研究,然后对鳄鱼进行排列,从小到大,坐标小的放前面,坐标大的放后面,然后我们还需要进行判断,哪些是能第一次跳到的鳄鱼,然后对其进行递归,我们还需要定义一个路径,跳的下一个鳄鱼的前面一个鳄鱼我们需要存起来,然后定义走的鳄鱼数,然后第一次能跳上去的鳄鱼我们递归都会得到从这只鳄鱼我们需要跳几次才能跳到岸,最后我们还需要进行筛选,得到最小的路径并将其打印。

首先我们给出相关的定义:

#include<stdio.h>
#include<math.h>
typedef struct {int x,y;
}crocodile;
typedef struct {int pre;int total;
}crocodile2;
typedef struct {int data[105];int head,rear;
}Quene;
crocodile data[105];//存储鳄鱼的坐标
crocodile2 path[105];//里面含有上一个节点的坐标,以及到这一步最少的步数
int visit[105];
int n,d;
int first;//第一步能跳的鳄鱼个数
Quene q;

关于队列的操作

void init(Quene &q) {q.head=q.rear=-1;
}
bool isempty(Quene q) {return q.head==q.rear;
}
void push(Quene &q,int i) {q.data[++q.rear]=i;
}
int pop(Quene& q) {return q.data[++q.head];
}

然后是一些小函数

bool isok(crocodile a) {if(50-abs(a.x)<=d||50-abs(a.y)<=d) return true;return false;
}
void swapcrocodile(crocodile &a,crocodile &b) {crocodile temp=a;a=b;b=temp;
}
bool cmp(crocodile a,crocodile b) {return pow(a.x,2)+pow(a.y,2)>pow(b.x,2)+pow(b.y,2);
}
bool canjump(crocodile a,crocodile b) {return d*d>=pow(a.x-b.x,2)+pow(a.y-b.y,2);
}
void start() {init(q);for(int i=0; i<n; i++) {path[i].pre=-1;path[i].total=10000;}
}

程序主函数

int main() {scanf("%d%d",&n,&d);//输入鳄鱼个数和步长getxy();if(d>42.5) printf("1\n");else getpath();return 0;
}

输入坐标

void getxy() {int actn=0;for(int i=0; i<n; i++) {int x,y;scanf("%d%d",&x,&y);if(x*x+y*y>7.5*7.5&&50>abs(x)&&50>abs(y)) //鳄鱼在湖中且不在岛上{data[actn].x=x;data[actn++].y=y;}}n=actn;//删掉无用的鳄鱼for(int i=0; i<n; i++) {for(int j=i+1; j<n; j++) 	if(cmp(data[i],data[j])) //对鳄鱼坐标进行排列swapcrocodile(data[i],data[j]);if(pow(data[i].x,2)+pow(data[i].y,2)>(7.5+d)*(7.5+d)) {first=i;//first最终指向第一次能跳到的鳄鱼的最后一个break;}}
}

获得路径

void getpath() {if(first==0) //一只鳄鱼都跳不上去{printf("0\n");return;}start();//初始化for(int i=0; i<first; i++) visit[i]=BFS(i);//进行遍历int count=100,tempv=1000;for(int i=0; i<first; i++) {if(visit[i]!=-1) {if(path[visit[i]].total<count) //是有效的,记录了跳上岸的步数,找到最小的路径{count=path[visit[i]].total;tempv=visit[i];//指向跳到岸的最后那只鳄鱼}}}if(tempv==1000) printf("0\n");else {printf("%d\n",path[tempv].total+1);//那一层再加上1print(tempv);//打印}
}

遍历

int BFS(int f) {visit[f]=1;//先访问push(q,f);//入队path[f].total=1;//步数记为1if(isok(data[f]))//直接跳上去了 {return f;}while(!isempty(q)) {int v=pop(q);//出队for(int i=first; i<n; i++) {if(canjump(data[v],data[i])&&path[i].total>path[v].total+1) {visit[i]=1;push(q,i);path[i].pre=v;//是从v跳过来的path[i].total=path[v].total+1;//记录步数if(isok(data[i])) return i;}}}return -1;
}

打印

void print(int tempv) {if(tempv==-1) return;print(path[tempv].pre);//递归到最后一层,逆序打印printf("%d %d\n",data[tempv].x,data[tempv].y);
}

总代码如下

#include<stdio.h>
#include<math.h>
typedef struct {int x,y;
}crocodile;
typedef struct {int pre;int total;
}crocodile2;
typedef struct {int data[105];int head,rear;
}Quene;
crocodile data[105];//存储鳄鱼的坐标
crocodile2 path[105];//里面含有上一个节点的坐标,以及到这一步最少的步数
int visit[105];
int n,d;
int first;//第一步能跳的鳄鱼个数
Quene q;
void init(Quene &q) {q.head=q.rear=-1;
}
bool isempty(Quene q) {return q.head==q.rear;
}
void push(Quene &q,int i) {q.data[++q.rear]=i;
}
int pop(Quene& q) {return q.data[++q.head];
}
bool isok(crocodile a) {if(50-abs(a.x)<=d||50-abs(a.y)<=d) return true;return false;
}
void swapcrocodile(crocodile &a,crocodile &b) {crocodile temp=a;a=b;b=temp;
}
bool cmp(crocodile a,crocodile b) {return pow(a.x,2)+pow(a.y,2)>pow(b.x,2)+pow(b.y,2);
}
bool canjump(crocodile a,crocodile b) {return d*d>=pow(a.x-b.x,2)+pow(a.y-b.y,2);
}
void start() {init(q);for(int i=0; i<n; i++) {path[i].pre=-1;path[i].total=10000;}
}
int BFS(int f) {visit[f]=1;push(q,f);path[f].total=1;if(isok(data[f])) {return f;}while(!isempty(q)) {int v=pop(q);for(int i=first; i<n; i++) {if(canjump(data[v],data[i])&&path[i].total>path[v].total+1) {visit[i]=1;push(q,i);path[i].pre=v;path[i].total=path[v].total+1;if(isok(data[i])) return i;}}}return -1;
}
void print(int tempv) {if(tempv==-1) return;print(path[tempv].pre);printf("%d %d\n",data[tempv].x,data[tempv].y);
}
void getpath() {if(first==0) {printf("0\n");return;}start();for(int i=0; i<first; i++) visit[i]=BFS(i);int count=100,tempv=1000;for(int i=0; i<first; i++) {if(visit[i]!=-1) {if(path[visit[i]].total<count) {count=path[visit[i]].total;tempv=visit[i];}}}if(tempv==1000) printf("0\n");else {printf("%d\n",path[tempv].total+1);print(tempv);}
}
void getxy() {int actn=0;for(int i=0; i<n; i++) {int x,y;scanf("%d%d",&x,&y);if(x*x+y*y>7.5*7.5&&50>abs(x)&&50>abs(y)) {data[actn].x=x;data[actn++].y=y;}}n=actn;//录入并删掉无用节点for(int i=0; i<n; i++) {for(int j=i+1; j<n; j++) 	if(cmp(data[i],data[j])) swapcrocodile(data[i],data[j]);if(pow(data[i].x,2)+pow(data[i].y,2)>(7.5+d)*(7.5+d)) {first=i;break;}}//将能跳的鳄鱼由近到远排序
}
int main() {scanf("%d%d",&n,&d);getxy();if(d>42.5) printf("1\n");else getpath();return 0;
}

如果在PTA编译不成功的话,建议换一下c++的那个模式。

这篇关于07-图5 Saving James Bond - Hard Version (30分)(数据结构)(c语言)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/430108

相关文章

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

30常用 Maven 命令

Maven 是一个强大的项目管理和构建工具,它广泛用于 Java 项目的依赖管理、构建流程和插件集成。Maven 的命令行工具提供了大量的命令来帮助开发人员管理项目的生命周期、依赖和插件。以下是 常用 Maven 命令的使用场景及其详细解释。 1. mvn clean 使用场景:清理项目的生成目录,通常用于删除项目中自动生成的文件(如 target/ 目录)。共性规律:清理操作

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

2024网安周今日开幕,亚信安全亮相30城

2024年国家网络安全宣传周今天在广州拉开帷幕。今年网安周继续以“网络安全为人民,网络安全靠人民”为主题。2024年国家网络安全宣传周涵盖了1场开幕式、1场高峰论坛、5个重要活动、15场分论坛/座谈会/闭门会、6个主题日活动和网络安全“六进”活动。亚信安全出席2024年国家网络安全宣传周开幕式和主论坛,并将通过线下宣讲、创意科普、成果展示等多种形式,让广大民众看得懂、记得住安全知识,同时还

《数据结构(C语言版)第二版》第八章-排序(8.3-交换排序、8.4-选择排序)

8.3 交换排序 8.3.1 冒泡排序 【算法特点】 (1) 稳定排序。 (2) 可用于链式存储结构。 (3) 移动记录次数较多,算法平均时间性能比直接插入排序差。当初始记录无序,n较大时, 此算法不宜采用。 #include <stdio.h>#include <stdlib.h>#define MAXSIZE 26typedef int KeyType;typedef char In

Maven创建项目中的groupId, artifactId, 和 version的意思

文章目录 groupIdartifactIdversionname groupId 定义:groupId 是 Maven 项目坐标的第一个部分,它通常表示项目的组织或公司的域名反转写法。例如,如果你为公司 example.com 开发软件,groupId 可能是 com.example。作用:groupId 被用来组织和分组相关的 Maven artifacts,这样可以避免

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

MiniGPT-3D, 首个高效的3D点云大语言模型,仅需一张RTX3090显卡,训练一天时间,已开源

项目主页:https://tangyuan96.github.io/minigpt_3d_project_page/ 代码:https://github.com/TangYuan96/MiniGPT-3D 论文:https://arxiv.org/pdf/2405.01413 MiniGPT-3D在多个任务上取得了SoTA,被ACM MM2024接收,只拥有47.8M的可训练参数,在一张RTX