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 ...
随机推荐
- .net sqlite 内存溢出 问题的分析与解决。
一个小的工具网站,用了sqlite数据库,在并发小的情况一切正常,但是并发量上来之后,就报"out of memory"错误了. 分析了代码,就是很简单的根据一个条件取一段数据,并 ...
- RedHat系统文本界面安装图形界面方法
版本: Linux version 2.6.32-431.el6.x86_64 (mockbuild@x86-023.build.eng.bos.redhat.com) (gcc version 4. ...
- webpack的安装和运行
webpack依赖于node,为了可以正靠运行,必须依赖node环境.node环境为了可以真正的执行很多代码,必须其中包含各种依赖的包npm工县(node packages manager)所以在进行 ...
- 使用Choose构建分支动态语句
1.在接口写方法 public List<Employee> getEmpsByConditionChoose(Employee employee); 2 在映射文件中配置 <!-- ...
- douyu danmu test c#
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Text.Regul ...
- 浏览器端-W3School:JS & DOM 参考手册
ylbtech-浏览器端-W3School:JS & DOM 参考手册 1.返回顶部 1. JavaScript 参考手册 本部分提供完整的 JavaScript 参考手册: JavaScri ...
- C# 打印倒三角
void test6(int num) { try { #region 方法1 int maxstar = (num - 1) * 2 + 1; string line = ""; ...
- 批处理文件将多台连接的手机安装同一个APP
:adb devices > C:\Users\aaminaxu\Desktop\a.txt :1.首先获取连接到电脑的手机的设备信息将其保存到C盘的a.txt文件中::2.将保存的txt文件中 ...
- 强化学习之MDP
前言 最近又入坑RL了,要搞AutoML就要学会RL,真的是心累.. 正文 MDP里面比较重要的就是状态值函数和动作-状态值函数吧,然后再求最优状态值函数和最优动作状态值函数,状态值函数的公式推导一开 ...
- python+selenium下载文件——firefox
修改Firefox的相关配置. 1.profile.set_preference('browser.download.folderList',2) 设置成0代表桌面,1代表下载到浏览器默认下载路径:2 ...