问题标签 [towers-of-hanoi]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
python - 循环(单向)河内塔建议?
这是我为河内塔问题编写的 Python 代码,其中必须将塔从左边的钉子转移到中间的钉子上,使用右边的钉子作为备用:
现在我的教授希望我以合法移动仅从塔 1 到 2、塔 2 到 3 和塔 3 到 1 的方式实施它。所有其他规则都是相同的。
c++ - 在 C++ 中使用 this 时出错
在这一行:move(n-1,dest,this);
得到一个错误,说对非 const 的引用的初始值必须是左值;
java - 河内塔在java中的回归
我的问题是第一个 System.out 语句下的“返回”是什么?
调试时,第solveTowers
一种方法到达基本情况后,即磁盘== 1,进入if语句,到达后return;
,进入第二System.out
条语句,然后是second solveTowers
方法,所以问题是为什么return;
跳过但first solveTowers
不是第二个?
python - 河内塔的解释
我是 python 的初学者,想知道if
语句在这个 Towers of Hanoi 函数中的作用:
java - 如何实例化 int 类型的 Stacks 数组?
我正在尝试创建一个堆栈数组,其中数组中的每个堆栈都是int
.
如果我像这样创建数组: Stack<Integer>[] numbers = new Stack<Integer>[3];
,则会出现编译错误“ Cannot create a generic array of Stack<Integer>
”。所以,我试图用通配符类型而不是创建堆栈数组Integer
,然后它就没有这个错误。
但是,如果我然后尝试像这样将一个int
堆栈(通配符“ ?
”类型)推入其中一个: this.numbers[stackIndex].push(i);
,则会出现编译错误“ The method push(capture#1-of ?) in the type Stack<capture#1-of ?> is not applicable for the arguments (int)
”。
那么,我怎样才能正确地实例化一个 Stacks 类型的数组int
呢?截至目前,我无法在这些堆栈上执行推送/弹出操作......
我的推理是尝试对河内塔游戏进行编程。我希望三个杆中的每一个都是 a
Stack
类型int
,每个环都表示为int
,并且三个杆一起包含为三个 Stacks 的数组。
这是一些示例代码:
c++ - 河内之塔,一举一动
我需要编写一个递归的河内塔程序,我已经完成了,但是它需要输出由星星代表的圆盘的位置,这就是我遇到的麻烦。每次进行移动时,它都需要输出光盘位置的图片表示。我为 3 张光盘编写了代码,但我需要它更灵活并适用于 3、4 或 5 张光盘。这是我目前拥有的代码。
python - 如何从递归结束时消除“无”
我有以下代码:
以下代码返回解决具有 n 个磁盘的河内塔所需的移动,并计算成功需要多少磁盘,但在所有移动之后,有一个“无”我想摆脱这个 - 你能帮忙吗?
c++ - 河内塔的组合量?
最近我在接受 C++ 开发者职位的面试,我被要求编写一个程序来解决一个有 3 列和 1000000 个圆盘的河内塔谜题,该程序必须将移动输出写入磁盘(“1->3”, “1->2”,...等等),我告诉他们这将是一个非常大的解决方案文件,因为河内塔的最小移动量是 2 次方 n - 1 而对于 1000000 这将是非常不适合任何硬盘驱动器的大数字,他们说经典算法是错误的,并且有一种算法可以解决这个难题,即使是 1000000 个带有发烧动作的光盘。我想知道是否存在这样的算法或者他们只是在骗我?
谢谢,帖木儿。
performance - 使用 Towers of Henoi 解决方案的意外性能数字
这是一个性能难题:我在一个Intel Celeron 1.8GHz, 3GB RAM, Ubuntu 12.04 32-bit
盒子上跑步。我在, &中编写了Towers of Henoi
递归解决方案。我跑了同样的路,震惊地看到以下时间:C
Python
Java
n = 25
我认为它的Java
性能会比这个盒子好,Python
但看起来Java
比这个盒子慢几个数量级。为了中和任何代码级别的性能影响,我将代码保留为最基本的形式。以下是3个解决方案:C
Python
henoi.c
henoi.py
Henoi.java
我正在使用gcc version 4.6.3
,Python 2.7.3
和Sun Java 1.6.0_24
.
有人可以帮我理解上面的数字吗???
勘误: 很久以后我才注意到我到处都将“Hanoi”拼写为“Henoi”。对不起 !
facebook - Facebook sample puzzle: Towers of Hanoi
Here is a question from Facebook hiring sample test.
There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs, output the moves required to transform from the initial to final configuration. You are required to do the transformations in minimal number of moves.
A move consists of picking the topmost disc of any one of the pegs and placing it on top of any other peg. At any point of time, the decreasing radius property of all the pegs must be maintained.
Constraints:
1<= N<=8
3<= K<=5
Input Format:
N K
2nd line contains N integers. Each integer is in the range 1 to K where the i-th integer denotes the peg to which disc of radius i is present in the initial configuration.
3rd line denotes the final configuration in a format similar to the initial configuration.
Output Format:
The first line contains M - The minimal number of moves required to complete the transformation.
The following M lines describe a move, by a peg number to pick from and a peg number to place on. If there are more than one solutions, it's sufficient to output any one of them. You can assume, there is always a solution with less than 7 moves and the initial confirguration will not be same as the final one.
Sample Input #00:
2 3
1 1
2 2
Sample Output #00:
3
1 3
1 2
3 2
Sample Input #01:
6 4
4 2 4 3 1 1
1 1 1 1 1 1
Sample Output #01:
5
3 1
4 3
4 1
2 1
3 1
There is no harm in discussing solution for this problem as it is a sample problem.
The solution to the classic Towers of Hanoi problem is really simple to code:
The above can also be extended to a general 'k' pegs tower of hanoi. But, this knowledge is turning out to be not at all useful to design a solution to this sample puzzle. Any suggestions as to how to approach this and similar kind of problems in future?