复杂的河内塔?

编程入门 行业动态 更新时间:2024-10-18 12:34:33
本文介绍了复杂的河内塔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近解决的问题河内塔。我用了一个分而治之的策略来解决这个问题。我分的主要问题为三个更小的子问题,因此下面生成复发。

I was recently solving Towers of Hanoi problem. I used a "Divide and Conquer" Strategy to solve this problem. I divided the main problem into three smaller sub problems and thus following recurrence was generated.

T(n)的2T =(N-1)1

T(n)=2T(n-1)+1

解决这个导致

O(2 ^ N)指数时间]

O(2^n) [exponential time]

然后我试图用记忆化技术来解决这个问题,但同样的空间复杂度是指数和堆空间耗尽很快和问题仍然无法解决对于更大的n。

Then i tried to use memoization technique to solve it, but here too the space complexity was exponential and heap space exhausted very soon and problem was still unsolvable for larger n.

有没有办法来解决这个问题,在不到指数的时间?什么是它的问题是可以解决的最佳时机?

Is there a way to solve the problem in less than exponential time? What is the best time in which the problem can be solved?

推荐答案

这取决于你所说的解决的意思。汉诺塔的问题,3钉和 N 磁盘会占用 2 ** N - 1 移动来解决,因此,如果要列举的动作,你显然不能做得比 0(2 ** N)自枚举 K 事情是 O(K)。

It depends what you mean by "solved". The Tower of Hanoi problem with 3 pegs and n disks takes 2**n - 1 moves to solve, so if you want to enumerate the moves, you obviously can't do better than O(2**n) since enumerating k things is O(k).

在另一方面,如果你只是想知道所需的移动次数(不枚举它们),计算 2 ** N - 1 是一个更快的操作

On the other hand, if you just want to know the number of moves required (without enumerating them), calculating 2**n - 1 is a much faster operation.

另外值得一提的,招式枚举可以反复使用 O(N)空间复杂度做了如下(disk1的最小盘):

Also worth noting, the enumeration of the moves can be done iteratively with O(n) space complexity as follows (disk1 is the smallest disk):

while true: if n is even: move disk1 one peg left (first peg wraps around to last peg) else: move disk1 one peg right (last peg wraps around to first peg) if done: break else: make the only legal move not involving disk1

更多推荐

复杂的河内塔?

本文发布于:2023-11-29 16:18:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:河内

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!