• 题目来源:

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

  • 题目描述:





  • 题目思路:

这道题目本质上讲就是列出图的连通集,但是这个连通集的起点是有约束的:詹姆斯邦德必须第一跳能跳到的点才是连通集的起点。解决这道问题可以使用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. POJ 2778 DNA Sequence ( Trie图、矩阵快速幂 )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析: 我们先分析Tire 图的结构 : Trie图是在AC自动机的原型上增添边使得状态可以快速转移,标记危 ...

  2. 将数据库中带出的列,在gridview中影藏起来

    前台增加事件:OnRowCreated="GridView1_RowCreated" protected void GridView1_RowCreated(object send ...

  3. 【洛谷T89353 【BIO】RGB三角形】

    题目链接 这个题我一开始显然直接暴力 然后30分(但是应用数据分治的我通过复杂度判断并且其余输出0的能力硬生生的拿下了60分) 主要还是讲正解 这里有一个结论 这样一个图,红点的值可以通过两个黄点来判 ...

  4. 初识Nginx及其LNMP搭建

    Nginx介绍 nginx www服务软件 俄罗斯人开发 开源 性能很高 web产品 大小780k c语言开发 本身是一款静态www软件,不能解析php jsp .do 最大特点 静态小文件(1m), ...

  5. Linux_SELinux使用

    目录 目录 SELinux SElinux的应用 修改 SELinux 下次启动模式 修改 SELinux 上下文 上下文的快速模仿 SELinux布尔值 图形化管理SElinux SELinux错误 ...

  6. 阶段3 1.Mybatis_03.自定义Mybatis框架_5.自定义Mybatis的编码-创建两个默认实现类并分析类之间的关系

    把XMLConfigBuilder的包名补全 这样我们就可以调用里面的loadConfiguration方法了 创建工厂实现类 实现SqlSessionFactory的接口 实现接口里面的方法 把cf ...

  7. 项目中引入kafka

    项目如果需要引入kafka,可以从以下几个流程走: 1.pom文件引对应的jar包 <dependency> <groupId>org.apache.kafka</gro ...

  8. JavaDoc注释

    标签 说明 JDK 1.1 doclet 标准doclet 标签类型 @author 作者 作者标识 √ √ 包. 类.接口 @version 版本号 版本号 √ √ 包. 类.接口 @param 参 ...

  9. SQL如何通过当前日期获取上周一日期【转】

    --当前时间 select getdate() --当前时间周的起始日期(以周一为例) ,) --上周起始: ,,)) --上上周起始: ,,)) --上上上周起始:s elect ,,))

  10. 转:mysql datetime类型精确到毫秒、微秒的问题

    原文地址:mysql datetime类型精确到毫秒.微秒的问题 mysql里面的datetime类型的精确度是可以到1/ 10 ^ 6 秒的某些客户端(如navicat for mysql)的显示经 ...