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

相关文章

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

shell脚本自动删除30天以前的文件(最新推荐)

《shell脚本自动删除30天以前的文件(最新推荐)》该文章介绍了如何使用Shell脚本自动删除指定目录下30天以前的文件,并通过crontab设置定时任务,此外,还提供了如何使用Shell脚本删除E... 目录shell脚本自动删除30天以前的文件linux按照日期定时删除elasticsearch索引s

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

C语言小项目实战之通讯录功能

《C语言小项目实战之通讯录功能》:本文主要介绍如何设计和实现一个简单的通讯录管理系统,包括联系人信息的存储、增加、删除、查找、修改和排序等功能,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录功能介绍:添加联系人模块显示联系人模块删除联系人模块查找联系人模块修改联系人模块排序联系人模块源代码如下

基于Go语言实现一个压测工具

《基于Go语言实现一个压测工具》这篇文章主要为大家详细介绍了基于Go语言实现一个简单的压测工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录整体架构通用数据处理模块Http请求响应数据处理Curl参数解析处理客户端模块Http客户端处理Grpc客户端处理Websocket客户端