• 题目来源:

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

  • 题目描述:





  • 题目思路:

这道题目本质上讲就是列出图的连通集,但是这个连通集的起点是有约束的:詹姆斯邦德必须第一跳能跳到的点才是连通集的起点。解决这道问题可以使用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. Python深度学习读书笔记-6.二分类问题

    电影评论分类:二分类问题   加载 IMDB 数据集 from keras.datasets import imdb (train_data, train_labels), (test_data, t ...

  2. idea中git回退本地仓库版本

    场景:代码commit到本地仓库,还没有push到远程仓库,这时要回退代码. 介绍下Reset Head中三种Reset Type类型: 1.Mixed(默认):它回退到某个版本,本地会保留源码,回退 ...

  3. Jmeter接口测试系列之保存断言结果到文件

    在执行完接口测试用例后,我们需要将失败的用例结果统一保存到文件中,可以使用“断言结果”组件,并定制输出内容. 1.配置断言结果组件输出 (1.在文件名中配置需要保存的文件路径和文件名: (2.勾选仅日 ...

  4. 中国MOOC_零基础学Java语言_第2周 判断_2信号报告

    2 信号报告(5分) 题目内容: 无线电台的RS制信号报告是由三两个部分组成的: R(Readability) 信号可辨度即清晰度. S(Strength)    信号强度即大小. 其中R位于报告第一 ...

  5. C# 打印倒三角

    void test6(int num) { try { #region 方法1 int maxstar = (num - 1) * 2 + 1; string line = ""; ...

  6. 【Airtest】由于Airtest中long_click无法实现长按,教你如何在Airtest中实现长按的方法

    Airtest中我们想要实现长按操作,poco中有一个方法long_click,但是实际使用了一下,发现并没有卵用,仍然是单击操作,如下图 那我们要如何进行长按操作呢?其实可以利用swipe实现,以长 ...

  7. pandas DataFram的insert函数

    原文链接:https://blog.csdn.net/yanwucao/article/details/80211984 DataFrame.insert(loc, column, value, al ...

  8. js while循环

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. 所遇Oracle错误代码

    ORA-00905: 缺失关键字   少了空格或关键字写错 ORA-00922: 选项缺失或无效 错误原因:一般是语句的语法有问题.比如命名不对,关键字写错等等.对于非标准的命名,一般采用双引号来创建 ...

  10. linux 通配符与正则表达式

    linux通配符和三剑客(grep.awk.sed)正则表达式是不一样的 通配符一般用户命令行bash环境,而linux正则表达式用于awk.grep.sed