Saving James Bond - Easy Version
- 题目来源:
浙江大学在慕课网上开设的《数据结构》课,陈越老师、何钦铭老师主讲,课后作业的一道题。
题目描述:
题目思路:
这道题目本质上讲就是列出图的连通集,但是这个连通集的起点是有约束的:詹姆斯邦德必须第一跳能跳到的点才是连通集的起点。解决这道问题可以使用DFS。
- C语言实现:
错误代码如下:
//孤岛应该被作为单独一个节点来测试
//孤岛周围可能有很多鳄鱼,程序就是要考察这些鳄鱼(节点)的连通集
//里有没有可以跳到岸上的。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#define MaxPointer 100
struct Pointer
{
int x;
int y;
};
struct Pointer Graph[MaxPointer];
bool Visited[MaxPointer]; //存储点是否被踩过
int jumpmaximum = 0; //007可以跳的最远距离
int pointernum = 0;
//作用:判断从中心可以调到那个鳄鱼头上
bool FirstJump(int i)
{
int dis = 0;
dis = (Graph[i].x - 0) * (Graph[i].x - 0) + (Graph[i].y - 0) * (Graph[i].y - 0);
return ((jumpmaximum + 7.5) * (jumpmaximum + 7.5) >= dis ? true : false);
}
//作用:判断从当前点能否跳到岸上去
//返回值: true 能
// false 不能
bool IsSafe(int i)
{
if (Graph[i].x + jumpmaximum >= 50 || Graph[i].x - jumpmaximum <= -50)
{
if (Graph[i].y + jumpmaximum >= 50 || Graph[i].y - jumpmaximum <= -50)
{
return true;
}
}
return false;
}
//作用:判断能否从i点跳到j点
//返回值: true 能
// false 不能
bool Jump(int i,int j)
{
int dis = 0;
dis = (Graph[i].x - Graph[j].x) * (Graph[i].x - Graph[j].x) + (Graph[i].y - Graph[j].y) * (Graph[i].y - Graph[j].y);
return (jumpmaximum * jumpmaximum >= dis ? true : false);
}
bool DFS(int i)
{
bool answer = false;
int j = 0;
//printf("%d.\n",i);
Visited[i] = true; //表示i点已经踩过
//能不能从当前点跳到岸上去
if (IsSafe(i))
{
answer = true;
}
for (j = 0; j < pointernum; j++)
{
if (!Visited[j] && Jump(i, j))
{
answer = DFS(j);
Visited[j] = false;
if (answer == true)
{
break;
}
}
}
return answer;
}
void Save007()
{
bool answer = false;
for (int i = 0;i < pointernum;i++)
{
if (!Visited[i] && FirstJump(i))
{
answer = DFS(i);
if (answer)
{
break;
}
}
}
if (answer)
{
printf("Yes");
}
else
{
printf("No");
}
}
int main()
{
scanf("%d", &pointernum);
scanf("%d", &jumpmaximum);
//初始化所有顶点状态都是未访问过状态
for (int i = 0; i < pointernum; i++)
{
Visited[i] = false;
}
for (int i = 0;i < pointernum;i++)
{
scanf("%d %d",&Graph[i].x,&Graph[i].y);
}
if (jumpmaximum >= 42.5)
{
printf("Yes");
}
Save007();
system("pause");
return 0;
}
最终修改BUG后的版本:
//孤岛应该被作为单独一个节点来测试
//孤岛周围可能有很多鳄鱼,程序就是要考察这些鳄鱼(节点)的连通集
//里有没有可以跳到岸上的。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#define MaxPointer 100
struct Pointer
{
int x;
int y;
};
struct Pointer Graph[MaxPointer];
bool Visited[MaxPointer]; //存储点是否被踩过
int jumpmaximum = 0; //007可以跳的最远距离
int pointernum = 0;
//作用:判断从中心可以调到那个鳄鱼头上
bool FirstJump(int i)
{
//int dis = 0;
//dis = (Graph[i].x - 0) * (Graph[i].x - 0) + (Graph[i].y - 0) * (Graph[i].y - 0);
//return ((jumpmaximum + 7.5) * (jumpmaximum + 7.5) >= dis ? true : false);
int p1 = pow(Graph[i].x, 2);
int p2 = pow(Graph[i].y, 2);
int r = (jumpmaximum + 7.5) * (jumpmaximum + 7.5);
if (p1 + p2 <= r) {
return true;
}
return false;
}
//作用:判断从当前点能否跳到岸上去
//返回值: true 能
// false 不能
bool IsSafe(int i)
{
if (Graph[i].x + jumpmaximum >= 50 || Graph[i].x - jumpmaximum <= -50
|| Graph[i].y + jumpmaximum >= 50 || Graph[i].y - jumpmaximum <= -50)
{
return true;
}
return false;
}
//作用:判断能否从i点跳到j点
//返回值: true 能
// false 不能
bool Jump(int i,int j)
{
int dis = 0;
dis = (Graph[i].x - Graph[j].x) * (Graph[i].x - Graph[j].x) + (Graph[i].y - Graph[j].y) * (Graph[i].y - Graph[j].y);
return (jumpmaximum * jumpmaximum >= dis ? true : false);
}
bool DFS(int i)
{
bool answer = false;
int j = 0;
//printf("%d.\n",i);
Visited[i] = true; //表示i点已经踩过
//能不能从当前点跳到岸上去
if (IsSafe(i))
{
answer = true;
}
for (j = 0; j < pointernum; j++)
{
if (!Visited[j] && Jump(i, j))
{
answer = DFS(j);
Visited[j] = false;
if (answer == true)
{
break;
}
}
}
return answer;
}
void Save007()
{
bool answer = false;
for (int i = 0;i < pointernum;i++)
{
if (!Visited[i] && FirstJump(i))
{
answer = DFS(i);
if (answer)
{
break;
}
}
}
if (answer)
{
printf("Yes");
}
else
{
printf("No");
}
}
int main()
{
scanf("%d", &pointernum);
scanf("%d", &jumpmaximum);
//初始化所有顶点状态都是未访问过状态
for (int i = 0; i < pointernum; i++)
{
Visited[i] = false;
}
for (int i = 0;i < pointernum;i++)
{
scanf("%d %d",&Graph[i].x,&Graph[i].y);
}
if (jumpmaximum >= 42.5)
{
printf("Yes");
}
Save007();
//system("pause");
return 0;
}
这两个程序主要的差别在最后判断邦德能不能直接从鳄鱼头跳到岸上去,第一个有BUG的版本中,这个判断函数是这样写的:
//作用:判断从当前点能否跳到岸上去
//返回值: true 能
// false 不能
bool IsSafe(int i)
{
if (Graph[i].x + jumpmaximum >= 50 || Graph[i].x - jumpmaximum <= -50)
{
if (Graph[i].y + jumpmaximum >= 50 || Graph[i].y - jumpmaximum <= -50)
{
return true;
}
}
return false;
}
第二个版本中,对这个函数进行了修改:
//作用:判断从当前点能否跳到岸上去
//返回值: true 能
// false 不能
bool IsSafe(int i)
{
if (Graph[i].x + jumpmaximum >= 50 || Graph[i].x - jumpmaximum <= -50
|| Graph[i].y + jumpmaximum >= 50 || Graph[i].y - jumpmaximum <= -50)
{
return true;
}
return false;
}
这两个版本的函数实现区别可以看下面的图,第一个版本的函数遗漏了一些点,所以才导致提交不通过。
Saving James Bond - Easy Version的更多相关文章
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- pat05-图2. Saving James Bond - Easy Version (25)
05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33
06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...
- PAT Saving James Bond - Easy Version
Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...
- 06-图2 Saving James Bond - Easy Version
题目来源:http://pta.patest.cn/pta/test/18/exam/4/question/625 This time let us consider the situation in ...
- PTA 06-图2 Saving James Bond - Easy Version (25分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 06-图2 Saving James Bond - Easy Version (25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 06-图2 Saving James Bond - Easy Version (25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 06-图2 Saving James Bond - Easy Version (25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 06-图2 Saving James Bond - Easy Version(25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
随机推荐
- &&的运算顺序
先判断“&&”左侧的表达式,左侧的表达式为真时,再运算右侧的表达式.如左侧为假,则不运算右侧.
- 详解设备PID和VID
根据USB规范的规定,所有的USB设备都有供应商ID(VID)和产品识别码(PID),主机通过不同的VID和PID来区别不同的设备. VID和PID都是两个字节长,其中,供应商ID(VID)由供应商向 ...
- 常见iPhone设备尺寸及分辨率(持续更新)
开发中常用的px与pt区别 px就是表示pixel,像素,是屏幕上显示数据的最基本的点: pt就是point,是印刷行业常用单位,等于1/72英寸. px全称为pixel,是一个点,它不是自然界的长度 ...
- BDD Cucumber 实战
cucumber cucumber 是一个用于执行 BDD 的自动化测试工具. 用户指南 创建 Spring Boot 项目并引入依赖 <?xml version="1.0" ...
- C#高级应用
学习编程也有大半年了,想起老师前天说的:“你们写的代码都是小儿科”,顿时有点心塞...想想也是,不可能就写一个For循环或者Foreach循环就可以拿到高薪了?我也觉得不太可能,下面我就来为大家简单介 ...
- visualSVN提交强制添加注释
Visual SVN Server下 右键项目 “所有任务”>“Manage Hooks” >选中Pre-commit hook然后edit编辑,添加如下代码 @echo off set ...
- go bigfile (文件传输管理系统)前端分片上传demo
BIGFILE Github地址: https://github.com/bigfile/bigfile 欢迎大家前来issue & star BIGFILE 中文文档地址:https://l ...
- struts2默认action设置了却访问不到
1.错误原因 我的package中共有两个action,第一个是默认action,用于访问的action不存在时候的出错处理,第二个是通配符方式写的action,name采用*_*形式的全通配符.配置 ...
- Elasticsearch5安装以及部署Head插件
请看完再动手,两篇文章都是找来的,合并在一起了,前半部分是参考,我是按照后半部分做的,而且执行中间也有坑. Elasticsearch5.X及 head插件 安装说明: 1.下载elasticsear ...
- 【BASIS系列】SAP 日志管理
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BASIS系列]SAP 日志管理 前言部分 ...