浙大数据结构第六周之Saving James Bond - Easy 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 whether or not he can escape.

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, print in a line "Yes" if James can escape, or "No" if not.

Sample Input 1:

14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12

Sample Output 1:

Yes

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

No

简单翻译:

就是在以所给的最远跳跃距离为半径这个限制,看能不能找到一条从湖心到湖岸的路径

主要思路:

(1)建图:建图的标准是能不能在两个节点之间跳跃,而判断能不能在两个节点之间跳跃的关键是将节点分为三类:岸,鳄鱼,岛

①:从岸边开始跳:注意岸是有半径的

②:最后跳上岸:注意只要x轴方向或者y轴方向有一个超过了边界就上岸了

③:在鳄鱼节点之间跳

实现上述判断的关键在于下面这个函数,其中都是考虑距离的平方:

bool CanJump(int jumpDistance, const Node* node1, const Node* node2) {  //判断能不能跳出分三种情况:从岛上开始跳,在两个鳄鱼之间跳,最后跳上岸bool flag = FALSE;double distanceSquareBetweenTwoNodes = DistanceSquare(node1, node2);if(node1->Xlabel == 0 && node1->Ylabel == 0 || node2->Xlabel == 0 && node2->Ylabel == 0) {  //注意,小岛是有半径的,判断如果其中有一个节点是岛if(distanceSquareBetweenTwoNodes <= pow(RADIUS + jumpDistance, 2)) {flag = TRUE;}}else if(node1->Xlabel == BOUNDRY && node2->Ylabel == BOUNDRY || node2->Xlabel == BOUNDRY && node2->Ylabel == BOUNDRY) { //传进来的节点之一是岸int x1 = node1->Xlabel;int x2 = node2->Xlabel;int y1 = node1->Ylabel;int y2 = node2->Ylabel;if(abs(x1 - x2) <= jumpDistance || abs(y1 - y2) <= jumpDistance) {flag = TRUE;}}else if(distanceSquareBetweenTwoNodes <= pow(jumpDistance, 2)) {    //如果两个节点都是鳄鱼节点flag = TRUE;}return flag;
}

 (二):搜索:

因为只要找到一条路径即可,而一定是从岛开始找,我选择了BFS(其实DFS也可以),用BFS搜索时,如果弹出的是岸就可以退出循环打印Yes,如果循环结束也没有找到打印No

第一次写错误:

相比之下,BFS到没那么难实现,真正难的倒是判断能不能在两个节点之间跳跃,一开始没注意岛的半径,然后又没有考虑跳上岸的条件

代码实现:

/*构造图,WEIGHT的标准是看能不能跳到*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_NODE_NUMS 105
#define RADIUS 7.5
#define BOUNDRY 50
#define TRUE 1
#define FALSE 0
#define NONE -1
typedef int bool;
/*构造图*/
/*图的数据结构*/
typedef struct MatrixGraphNode MatrixGraphNode;
typedef MatrixGraphNode* MatrixGraph;
struct MatrixGraphNode {int NodeNums, EdgeNums;int Weight[MAX_NODE_NUMS][MAX_NODE_NUMS];
};
typedef struct Node Node;
struct Node {double Xlabel, Ylabel;
};
MatrixGraph CreateEmptyGraph (int nodeNums) {MatrixGraph graph = (MatrixGraph)malloc(sizeof(MatrixGraphNode));graph->NodeNums = nodeNums;for(int i = 0; i < nodeNums; i++) {for(int j = 0; j < nodeNums; j++) {graph->Weight[i][j] = FALSE;}}return graph;
}
void InsertEdge(int start, int end, MatrixGraph graph) {graph->Weight[start][end] = TRUE;graph->Weight[end][start] = TRUE;return;
}
double DistanceSquare(const Node* node1, const Node* node2) {double xLabel1 = node1->Xlabel;double yLabel1 = node1->Ylabel;double xLabel2 = node2->Xlabel;double yLabel2 = node2->Ylabel;double xDifference = xLabel1 - xLabel2;double yDifference = yLabel1 - yLabel2;return pow(xDifference, 2) + pow(yDifference, 2);
}
/*
如果有鳄鱼在小岛上
如果有鳄鱼在岸上
*/
bool CanJump(int jumpDistance, const Node* node1, const Node* node2) {  //判断能不能跳出分三种情况:从岛上开始跳,在两个鳄鱼之间跳,最后跳上岸bool flag = FALSE;double distanceSquareBetweenTwoNodes = DistanceSquare(node1, node2);if(node1->Xlabel == 0 && node1->Ylabel == 0 || node2->Xlabel == 0 && node2->Ylabel == 0) {  //注意,小岛是有半径的,判断如果其中有一个节点是岛if(distanceSquareBetweenTwoNodes <= pow(RADIUS + jumpDistance, 2)) {flag = TRUE;}}else if(node1->Xlabel == BOUNDRY && node2->Ylabel == BOUNDRY || node2->Xlabel == BOUNDRY && node2->Ylabel == BOUNDRY) { //传进来的节点之一是岸int x1 = node1->Xlabel;int x2 = node2->Xlabel;int y1 = node1->Ylabel;int y2 = node2->Ylabel;if(abs(x1 - x2) <= jumpDistance || abs(y1 - y2) <= jumpDistance) {flag = TRUE;}}else if(distanceSquareBetweenTwoNodes <= pow(jumpDistance, 2)) {    //如果两个节点都是鳄鱼节点flag = TRUE;}return flag;
}
MatrixGraph BuildGraph(int nodeNums, int jumpDistance) {int crocodileNums = nodeNums - 2;MatrixGraph graph = CreateEmptyGraph(nodeNums);Node allNode[nodeNums];allNode[0].Xlabel = 0;allNode[0].Ylabel = 0;allNode[nodeNums - 1].Xlabel = BOUNDRY;allNode[nodeNums - 1].Ylabel = BOUNDRY;for(int i = 1; i <= crocodileNums; i++) {scanf("%lf %lf", &allNode[i].Xlabel, &allNode[i].Ylabel);}for(int i = 0; i < nodeNums; i++) {for(int j = i; j < nodeNums; j++) {if(CanJump(jumpDistance, &allNode[i], &allNode[j])) {InsertEdge(i, j, graph);}}}return graph;
}
/*构造队列的数据结构*/
typedef struct QueueNode QueueNode;
typedef QueueNode* Queue;
struct QueueNode {int Data[MAX_NODE_NUMS];int Head, Rear, Size;
};
void InitQueue(Queue* q) {(*q)->Head = 0;(*q)->Rear = -1;for(int i = 0; i < MAX_NODE_NUMS; i++) {(*q)->Data[i] = NONE;}
}
bool IsFull(Queue* q) {if((*q)->Size == MAX_NODE_NUMS) {return TRUE;}else {return FALSE;}
}
bool IsEmpty(Queue* q) {if((*q)->Size == 0) {return TRUE;}else {return FALSE;}
}
int Dequeue(Queue* q) {if(IsEmpty(q)) {return NONE;}else {int ret = (*q)->Data[(*q)->Head++ % MAX_NODE_NUMS];(*q)->Size--;(*q)->Data[(*q)->Head - 1] = NONE;return ret;}
}
void Enqueue(Queue* q, int data) {if(!IsFull(q)) {(*q)->Data[++(*q)->Rear % MAX_NODE_NUMS] = data;(*q)->Size++;return;}
}
/*BFS*/
bool Vis[MAX_NODE_NUMS];
bool BFS(MatrixGraph graph, int index, int nodeNums) {Queue q = (Queue)malloc(sizeof(QueueNode));InitQueue(&q);Enqueue(&q, index);while(!IsEmpty(&q)) {int root = Dequeue(&q);if(root == NONE) {break;}if(root == nodeNums - 1) {return TRUE;}Vis[root] = TRUE;for(int i = 0; i < nodeNums; i++) {if(Vis[i] == FALSE && graph->Weight[root][i] == TRUE) {Vis[i] = TRUE;Enqueue(&q, i);}}}return FALSE;
}
/*判断能不能救出007*/
void Save007(MatrixGraph graph, int nodeNums) {if(BFS(graph, 0, nodeNums)) {printf("Yes");}else {printf("No");}
}
int main() {int crocodileNums, jumpDistance;scanf("%d %d", &crocodileNums, &jumpDistance);int nodeNums = crocodileNums + 2;   //总结点数量加2是因为还要考虑岛节点和边节点MatrixGraph graph = BuildGraph(nodeNums, jumpDistance);Save007(graph, nodeNums);free(graph);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/18275.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Django学习记录:使用ORM操作MySQL数据库并完成数据的增删改查

Django学习记录&#xff1a;使用ORM操作MySQL数据库并完成数据的增删改查 数据库操作 MySQL数据库pymysql Django开发操作数据库更简单&#xff0c;内部提供了ORM框架。 安装第三方模块 pip install mysqlclientORM可以做的事&#xff1a; 1、创建、修改、删除数据库中的…

【腾讯云 Cloud studio 实战训练营】搭建Next框架博客——抛开电脑性能在云端编程(沉浸式体验)

文章目录 ⭐前言⭐进入cloud studio工作区指引&#x1f496; 注册coding账号&#x1f496; 选择cloud studio&#x1f496; cloud studio选择next.js&#x1f496; 安装react的ui框架&#xff08;tDesign&#xff09;&#x1f496; 安装axios&#x1f496; 代理请求跨域&#x…

动态爬虫IP与反爬虫技术的博弈:揭秘真实反爬虫事例引发的思考

作为一名长期从事爬虫行业动态IP解决方案服务商&#xff0c;我们深知动态IP代理在抗击反爬虫方面的重要性。在当今数字化时代&#xff0c;互联网数据的爆炸性增长让数据采集变得前所未有的重要。然而&#xff0c;随着数据价值的不断提升&#xff0c;反爬虫技术也日益增强&#…

分库分表之基于Shardingjdbc+docker+mysql主从架构实现读写分离(一)

说明&#xff1a;请先自行安装好docker再来看本篇文章&#xff0c;本篇文章主要实现通过使用docker部署mysql实现读写分离&#xff0c;并连接数据库测试。第二篇将实现使用Shardingjdbc实现springboot的读写分离实现。 基于Docker去创建Mysql的主从架构 #创建主从数据库文件夹…

Ubuntu 20.04 系统或图像界面卡死或完全无响应处理方法

Ubuntu 20.04 系统或图像界面卡死或完全无响应处理方法 问题背景无需重启方法安全重启方法 问题背景 Ubuntu 20.04在安装驱动程序时系统突然无响应&#xff0c;终端也无法运行&#xff1b;考虑到尽量不破坏系统&#xff0c;不希望强制上下电重启机器&#xff0c;以免损坏文件系…

版本控制和团队协作:前端工程化的关键要素

文章目录 版本控制系统介绍&#xff08;如 Git&#xff09;1. 分布式系统2. 分支管理3. 版本控制4. 快速和高效5. 社区和生态系统 分支管理和团队协作流程1. 主分支2. 功能分支3. 开发工作4. 合并到develop5. 发布准备6. 发布 持续集成与持续部署实践持续集成&#xff08;CI&am…

【前端知识】React 基础巩固(三十七)——自定义connect高阶组件

React 基础巩固(三十七)——自定义connect高阶组件 一、手撸一个自定义connect高阶组件 import { PureComponent } from "react"; import store from "../store";/*** connect的参数&#xff1a;* 参数一&#xff1a; 函数* 参数二&#xff1a; 函数* 返…

lc1074.元素和为目标值的子矩阵数量

创建二维前缀和数组 两个for循环&#xff0c;外循环表示子矩阵的左上角&#xff08;x1,y1&#xff09;&#xff0c;内循环表示子矩阵的右下角&#xff08;x2,y2&#xff09; 两个for循环遍历&#xff0c;计算子矩阵的元素总和 四个变量&#xff0c;暴力破解的时间复杂度为O(…

ChatGPT安全技术

前言 近期&#xff0c;Twitter 博主 lauriewired 声称他发现了一种新的 ChatGPT"越狱"技术&#xff0c;可以绕过 OpenAI 的审查过滤系统&#xff0c;让 ChatGPT 干坏事&#xff0c;如生成勒索软件、键盘记录器等恶意软件。 他利用了人脑的一种"Typoglycemia&q…

MySQL 5.7版本不支持ROW_NUMBER()函数

MySQL 5.7版本不支持ROW_NUMBER()函数。但是&#xff0c;你可以使用变量来手动实现这个功能。以下是一个示例查询语句&#xff1a; SELECT row_number:row_number1 AS row_num,column1, column2, ... FROM (SELECT row_number:0) AS t,your_table ORDER BY column1;在这个语句…

web3行业有哪些职业发展路径?

Web3 是一个相对较新的概念&#xff0c;因此其职业发展路径也在不断演变。一般来说&#xff0c;Web3 职业发展路径可以分为以下几个方向&#xff1a; 区块链开发工程师&#xff1a;区块链开发工程师需要掌握 Solidity 等语言和智能合约开发技能&#xff0c;负责开发和维护区块…

Vue.js2+Cesium 四、模型对比

Vue.js2Cesium 四、模型对比 Cesium 版本 1.103.0&#xff0c;低版本 Cesium 不支持 Compare 对比功能。 Demo 同一区域的两套模型&#xff0c;实现对比功能 <template><div style"width: 100%; height: 100%;"><divid"cesium-container"…

MTK联发科安卓核心板MT8385(Genio 500)规格参数资料_性能介绍

简介 MT8385安卓核心板 是一个高度集成且功能强大的物联网平台&#xff0c;具有以下主要特性&#xff1a; l 四核 Arm Cortex-A73 处理器 l 四核Arm Cortex-A53处理器 l Arm Mali™-G72 MP3 3D 图形加速器 (GPU)&#xff0c;带有 Vulkan 1.0、OpenGL ES 3.2 和 OpenCL™ 2.x …

tinyproxy搭建http代理

安装tinyproxy yum install tinyproxy 修改配置 vim /etc/tinyproxy/tinyproxy.conf # 允许任意ip访问 Allow 0.0.0.0/0 # 指定端口 ​​​​​​​Port 8888 常用配置 User nobody Group nobody# 绑定监听端口号 Port 8608 # 监听的网络接口 默认会监听所有的接口 #Liste…

Linux中的file命令:查看文件类型

2023年8月1日&#xff0c;周二上午 目录 简要说明使用方法MIME类型举例说明 简要说明 在Linux中&#xff0c;file命令用于识别文件类型。 file命令可以识别各种类型的文件&#xff0c;包括普通文件、目录、符号链接、设备文件、压缩文件、二进制可执行文件等。 它是一个非常…

云原生势不可挡,如何跳离云原生深水区?

云原生是云计算领域一大热词&#xff0c;伴随云原生概念而来的是数字产业迎来井喷、数字变革来临、数字化得以破局以及新一波的技术红利等等。云原生即“云”原生&#xff0c;顾名思义是让“应用”最大程度地利用云的能力&#xff0c;发挥云价值的最佳路径。具体来说&#xff0…

抄写Linux源码(Day2:构建调试环境)

我们计划把操作系统运行在 qemu-system-x86_64 上&#xff0c;使用 gdb 调试 经过 RTFM&#xff0c;可以使用 qemu-system-x86_64 -s -S 让 qemu 在启动之后停住 接着在另一个窗口运行 gdb&#xff0c;输入命令 target remote localhost:1234&#xff0c;即可连接qemu并调试运…

Zookeeper笔记

为什么要使用Zookeeper dubbo需要一个注册中心&#xff0c;而Zookeeper是我们在使用Dubbo是官方推荐的注册中心 Zookeeper介绍 Zookeeper的集群机制 Zookeeper是为了其他分布式程序提供服务的&#xff0c;所以不能随便就挂了。Zookeeper的集群机制采取的是半数存活机制。也…

【MySQL】下载安装以及SQL介绍

1&#xff0c;数据库相关概念 以前我们做系统&#xff0c;数据持久化的存储采用的是文件存储。存储到文件中可以达到系统关闭数据不会丢失的效果&#xff0c;当然文件存储也有它的弊端。 假设在文件中存储以下的数据&#xff1a; 姓名 年龄 性别 住址 张三 23 男 北京…

GitHub基本使用

GitHub搜索 直接搜索 直接搜索关键字 明确搜索仓库标题 语法&#xff1a;in:name [关键词]展示&#xff1a;比如我们想在GitHub仓库中标题中搜索带有SpringBoot关键词的&#xff0c;我们可以样搜: in:name SpringBoot 明确搜索描述 语法&#xff1a;in:description [关键词]展…