algorithm - 一条线上的两个机器人

标签 algorithm search

当您需要对它们进行编程以满足时,您可能知道两个机器人掉在一条线上的问题。

Two robots are dropped from an airplane and land on a single line (with discrete positions) using a parachute which is left at the landing point. The robots are both facing north, they are an unknown distance apart, and one has landed directly east of the other.

The robots are now to be programmed such that they meet each other. They can be instructed to move left or right to a neighboring position and to check whether a parachute is present at the current location. If the other robot is met both robots stop there and live happily ever after.

The parachute check might conditionally execute any number of instructions and any block of instructions may be repeated unconditionally. Write down a program that both robots can follow simultaneously and which garuantees that they meet.

您必须创建一个适用于两个机器人的通用算法(有点复杂),以保证机器人会相遇。他们将降落伞留在降落地点,他们可以检查当前位置是否有降落伞。

原始声明在这里:http://en.wikibooks.org/wiki/Puzzles/Logic_puzzles/Parachuted_Robots 还有一个我不明白的解决方案。如果有人能理解它,请帮我解释一下。任何其他解决方案将不胜感激。

我对这个问题的第一个想法是对机器人进行编程,使其随机选择先向右或向左移动,然后进行类似指数搜索的操作:首先向右移动 2 个位置,然后向左移动 4 个位置,依此类推。如果在一个位置在这种“旅行”中,机器人在右侧或左侧找到第二个降落伞(另一个机器人使用的那个),机器人将只在那个方向搜索。这有什么意义吗?

非常感谢!

最佳答案

我的程序实际上更短,而且也很有魅力:

start: left
skipNext
goto start
next: left
goto next

这是可行的,因为第二个循环比第一个循环快。

您可以在这里测试您的程序:http://david-peter.de/parachuting-robots/

关于algorithm - 一条线上的两个机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596473/

相关文章:

java - 使用java实现Trie

algorithm - 遍历所有节点的迭代加长伪代码

c++ - 数组 : Largest possible number

ruby - 查找数据库中大量数据的词频

java - StringBuffer 与 StringBuilder

javascript - 如何使用各种参数在 Mongoose 中进行搜索查询?

sql - 如何为可搜索性构建数据

algorithm - 检索包含指定点的矩形集

android - 如何将搜索 View 添加到我的 Android 应用程序?

PHP 从字符串中获取搜索词数组