汉诺塔

visio 2003-08-20 09:46:22
汉诺塔的非递归算法各位如何解决,例如用迭代
...全文
146 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshihuzi 2003-08-23
  • 打赏
  • 举报
回复
楼上能给出该问题的用算法描述么?让在下学习一下。
tabris17 2003-08-21
  • 打赏
  • 举报
回复
首先,迭代算法要解决的就是2个问题

本轮移动那个?往那里移?

第一个问题很简单,每个子最多3种状态:1 上轮刚动过,2 无法移动的,3 本轮应动的

第二个问题更简单:将棋子(姑且这么称)按从低到高编号(大到小),1,2,3……,轮到单号棋子动就是左移,双号动就是右移
Bigstorns 2003-08-20
  • 打赏
  • 举报
回复
楼上的写了这么多竟然看不懂
真是郁闷啊
!~~~~~
zeta999999 2003-08-20
  • 打赏
  • 举报
回复
On the Iterative Solution of the Towers of Hanoi Problem

Introduction. Recall the well-known problem of the Towers of Hanoi and its recursive
solution. Given are three places A,B,C and N disks, numbered 0. .N−1 in order of increasing
size, and initially they are stacked in decreasing size (smallest disk at the top) at place A.
The goal is to move the tower to place B, according to the following rules:
only one disk is moved at a time,
a larger disk is never placed on a smaller one, and
the disks are stacked only at the three given places.
Denoting a direct move of disk n from place a to b not touching c by (n, (a, b, c)), the shortest
sequence of moves to bring a tower of N disks from place A to B using place C along the
way, is expressed by HanoiN (A,B,C), where:
Hanoi0(a, b, c) = h i
Hanoin+1(a, b, c) = Hanoin(a, c, b) a h(n, (a, b, c))i a Hanoin(c, b, a)
The definition is non-trivially recursive, and is therefore not suitable as a recipe for human
beings. There exists an iterative solution, which is easy to execute by humans:
If N is odd, cycle the smallest disk through A,B,C (“clockwise”), and after every move
of the smallest disk, do one other move (for which there is only one possibility according
to the rule that a larger disk may not be placed on a smaller one).
If N is even, the smallest disk should be cycled through A,C,B (“anti-clockwise”).
We set out to formally prove that a crucial property of this iterative solution follows from
Hanoi’s definition: the smallest disk cycles in the way indicated. Actually, we prove in one
go, without extra effort, that all disks cycle. We do not prove that the smallest disk is moved
alternately with the others.
Anybody familiar with the principle of induction is able to prove the property themself
(try it!); so the existence of a proof is not at all surprising. What is surprising, however, is
the elegance of our proof: we use only elementary, everyday set calculus (expressed in the Z
notation [2]) and no case distinctions, and our proof is a calculation of a few lines long, and
1
consists of steps that are both “human readable” and “machine verifiable”. (Also, neither
powers-of-two nor lengths-of-sequences occur anywhere in our proof.) It was Backhouse’s
paper [1] (with a quite different formulation and proof of the same property) that prompted
us to do so.
Abstraction. As a preparation, we abstract from the places and keep only the direction of
the moves. To this end, we first write the definition of Hanoi as follows:
Hanoi0(d) = h i
Hanoin+1(d) = Hanoin(f d) a h(n, d)i a Hanoin(g d)
Here, d ranges over triples (a, b, c), and f = a, b, c • (a, c, b) and g = a, b, c • (c, b, a).
Now, identify (A,B,C), (B,C,A), (C,A,B) with each other and denote these with “direction
0”, and similarly for (A,C,B), (C,B,A), (B,A,C) and “direction 1”. Observe that,
under this identification, f and g become ‘the successor/predecessor modulo 2’, denoted with
a postfixed 1. Thus, we find the definition:
H0(d) = h i
Hn+1(d) = Hn(d1) a h(n, d)i a Hn(d1)
Here, and in the sequel, d varies over directions 0. . 1, and we let denote addition modulo 2.
We shall use the following laws from ‘mod 2’ calculus:
is associative and commutative
i n d = d if i = n
(n−1) d 1 = n d
Some set notation. (Here and in the remainder of the paper one may specialise “arbitrary
i” to just i = 0; our more general treatment is for free.)
We wish to talk about the set of directions of disk i in the sequence Hnd of moves: our
claim will be that this set is a singleton set if i < n (that is, “disk i cycles if it is one of the
disks 0 . . n−1”) and empty otherwise. More specifically, the singleton set contains direction
i (n−1) d, that is, “it contains direction d iff i and n−1 have the same parity”. In order
to express that singleton or empty set, recall Z’s set notation {D | P • E}, where D is a
collection of variable declarations, P is a predicate, and E is an expression; the set consists
of all values E where the variables range over sets as declared by D but only as far as they
satisfy predicate P. The set notation {| P • E} is a special case (neither tricky nor abuse
of notation, just a special case): it is equal to {E} if P holds and ? otherwise, and saves us
from explicitly making case distinctions. In particular, the set of interest is expressed by:
{| i<n • i (n−1) d}
It remains to define the set Di (S) of directions d that appear in an item (i, d) in sequence S.
Using the Z convention that a sequence is a function mapping indices to items, and that a
function is a set of (argument, result)-pairs, the definition reads:
Di (S) = {d | (i, d) 2 ran S}
2
= ran ({i}C ran S)
Here X CY is the Z notation for ‘domain restriction’: X CY = {u, v | u 2 X ^ (u, v) 2 Y }.
We shall use the following elementary properties about these notations:
ran hx i = {x}
ran (SaT) = ran S [ ranT
X C (Y [Z) = XCY [ XCZ
{i} C {(n, d)} = {| i = n • (n, d)}
ran{| P • (E,E0)} = {| P • E0}
{| P • E} [ {| Q • E} = {| P _ Q • E}
Theorem. For all naturals i and n:
Di (Hnd) = {| i<n • i (n−1) d}
Proof.
We use induction on n. For n = 0 we calculate:
Di (H0d)
= ran ({i}C ranh i) defs Di , H0
= ? ran on h i, XC on ?, ran on ?
= {| i < 0 • i (0−1) d} i 0
For n+1 > 0, not applying any other set theoretic law than mentioned above:
Di (Hn+1d)
= ran ({i}C (ran (Hn(d 1) a h(n, d)i a Hn(d 1)))) defs Di , Hn+1
= ran ({i}C (ranHn(d 1) [ ranh(n, d)i [ ranHn(d 1))) ran on a
= ran ({i}C (ranHn(d 1) [ ranh(n, d)i)) idempotency [
= ran ({i}C (ranHn(d 1) [ {(n, d)})) ran on h i
= ran ({i}C(ranHn(d 1)) [ {i}C{(n, d)}) {i}C on [
= ran ({i}C(ranHn(d 1)) [ {| i=n • (n, d)}) {i}C on { }
= ran({i}C(ranHn(d 1))) [ ran{| i=n • (n, d)} ran on [
= Di (Hn(d 1)) [ {| i=n • d} def Di , ran on {| }
= {| i<n • i (n−1) d 1} [ {| i=n • d} induction hypothesis
= {| i<n • i n d} [ {| i=n • i n d} ‘mod 2’ calculus
= {| i < n _ i = n • i n d} [ of similar sets
= {| i < n+1 • i ((n+1)−1) d} arithmetic
This completes the proof.
As corollary we have, for odd N, that D0(HN 0) = {0}: the smallest disk cycles in direction 0
(“clockwise” through A,B,C), and similarly for even N > 0 that D0(HN (0)) = {1}.

看不懂@.@
visio 2003-08-20
  • 打赏
  • 举报
回复
对,我就是从这本书上搬来的,可是并没有给出解法
zeta999999 2003-08-20
  • 打赏
  • 举报
回复
c how to program上说任何递归方法都可以用迭代来解决

这个问题我很久以前就问过,不过至今不知道结果
http://expert.csdn.net/Expert/topic/1900/1900586.xml?temp=.4679224
tabris17 2003-08-20
  • 打赏
  • 举报
回复
当然可以确定先移动到哪一个

小时候玩文曲星时就知道规律了
visio 2003-08-20
  • 打赏
  • 举报
回复
那可有别的算法
woshihuzi 2003-08-20
  • 打赏
  • 举报
回复
据我所知,没有办法迭代。因为,你无法确定先移动到哪一个。因此,无法迭代为低一阶的问题。

69,337

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧