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语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

Go语言网络故障诊断与调试技巧

《Go语言网络故障诊断与调试技巧》在分布式系统和微服务架构的浪潮中,网络编程成为系统性能和可靠性的核心支柱,从高并发的API服务到实时通信应用,网络的稳定性直接影响用户体验,本文面向熟悉Go基本语法和... 目录1. 引言2. Go 语言网络编程的优势与特色2.1 简洁高效的标准库2.2 强大的并发模型2.

Go语言使用sync.Mutex实现资源加锁

《Go语言使用sync.Mutex实现资源加锁》数据共享是一把双刃剑,Go语言为我们提供了sync.Mutex,一种最基础也是最常用的加锁方式,用于保证在任意时刻只有一个goroutine能访问共享... 目录一、什么是 Mutex二、为什么需要加锁三、实战案例:并发安全的计数器1. 未加锁示例(存在竞态)

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五