• 题目来源:

浙江大学在慕课网上开设的《数据结构》课,陈越老师、何钦铭老师主讲,课后作业的一道题。

  • 题目描述:





  • 题目思路:

这道题目本质上讲就是列出图的连通集,但是这个连通集的起点是有约束的:詹姆斯邦德必须第一跳能跳到的点才是连通集的起点。解决这道问题可以使用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的更多相关文章

  1. Saving James Bond - Easy Version (MOOC)

    06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...

  2. pat05-图2. Saving James Bond - Easy Version (25)

    05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...

  3. 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 ...

  4. PAT Saving James Bond - Easy Version

    Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. SRCNN 卷积神经网络

    2019-05-19 从GitHub下载了代码(这里) 代码量虽然不多,但是第一次学,花了时间还是挺多的.根据代码有跑出结果(基本没有改),但是对于数据集的处理还是看的很懵逼,主要是作者的实现都是用类 ...

  2. 555E Case of Computer Network

    分析 一个连通块内的肯定不影响 于是我们先缩点 之后对于每个路径 向上向下分别开一个差分数组 如果两个数组同时有值则不合法 代码 #include<bits/stdc++.h> using ...

  3. Mac OS 10安装CocoaPods流程

    一.简介 什么是CocoaPods CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPod ...

  4. 七、SpringBoot项目集成JSP以及项目不同启动方式及访问路径配置

    1.创建JSP目录 在src/main目录下创建目录webapp/WEB-INF/jsp用于存放jsp页面,如下图: 然后再改文件夹下面我们创建JSP文件: 大家在使用IDEA 的new菜单创建JSP ...

  5. Mac下安装lightgbm

    Mac下安装lightgbm 1.安装环境 系统 MacOS Mojave 版本10.14.2 Xcode 10.1 $ clang -v Apple LLVM version 10.0.0 (cla ...

  6. Delphi回车键切换焦点

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. CentOS安Elasticsearch

    工作中有需求用到es做数据分析和日志搜索的,整理记录一下安装部署过程.ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful we ...

  8. CSS3—— 2D转换 3D转换 过渡 动画

    2D转换 对元素进行移动.缩放.转动.拉长或拉伸 ————>  ————>   移动 顺时针旋转 扩大/缩小 倾斜 2D变换合并  3D转换 绕x轴 绕y轴 过渡 从一种样式逐渐改变为另一 ...

  9. spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现

    spring 定时任务 quartz 基于  MethodInvokingJobDetailFactoryBean 实现 依赖包 如下 <dependencies> <depende ...

  10. 语言I—2019秋作业02

    这个作业属于那个课程 这个作业要求在哪里 我在这个课程的目标是 这个作业在那个具体方面帮助我实现目标 参考文献 C语言程序设计I https://edu.cnblogs.com/campus/zswx ...