汉诺塔的c++算法(towers of hanoi)

May 30th, 2007
考试结束了整理了一下笔记, 发现这个比较有用, 于是做个存档。 汉诺塔也叫河内塔,是个比较简单的智力游戏。 汉诺塔的c++算法(towers of hanoi)


3个圆盘的汉诺塔的移动


4个圆盘的汉诺塔的移动

有三根杆子A,B,C。A杆上有N个(N>1)穿孔圆盘,盘的尺寸由下到上依次变小。要求按下列规则将所有圆盘移至C杆:

  1. 每次只能移动一个圆盘;
  2. 大盘不能叠在小盘上面。

提示:可将圆盘临时置于B杆,也可将从A杆移出的圆盘重新移回A杆,但都必须尊循上述两条规则。

问:如何移?最少要移动多少次?



<br/>#include &lt;iostream.h&gt;//towers of hanoi<br/>using namespace std;<br/><br/>int main()<br/>{<br/> &nbsp;void towers(int n, char t1, char t2, char t3);<br/> &nbsp;int n;<br/> &nbsp;cout &lt;&lt; &quot;enter the number of disks:&quot;;<br/> &nbsp;cin &gt;&gt; n;<br/> &nbsp;towers(n, &#039;A&#039;, &#039;B&#039;, &#039;C&#039;);<br/>}<br/><br/>void towers(int n, char t1, char t2, char t3)<br/>{<br/> &nbsp;if(n &lt; 1) cout &lt;&lt; &quot;no disks&quot;;<br/> &nbsp;else if(n == 1) cout &lt;&lt;&quot;move top disk on tower &quot; &lt;&lt; t1 &lt;&lt; &quot; to tower &quot; &lt;&lt; t2 &lt;&lt; endl;<br/> &nbsp;else<br/> &nbsp;{<br/> &nbsp; &nbsp;towers(n-1, t1, t3, t2);<br/> &nbsp; &nbsp;cout &lt;&lt;&quot;move top disk on tower &quot; &lt;&lt; t1 &lt;&lt; &quot; to tower &quot; &lt;&lt; t2 &lt;&lt; endl;<br/> &nbsp; &nbsp;towers(n-1, t3, t2, t1);<br/> &nbsp;}<br/> &nbsp;exit(0);<br/>}<br/>
Share on Facebook
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • Fleck
  • Gwar
  • Haohao
  • Identi.ca
  • laaik.it
  • LinkaGoGo
  • LinkedIn
  • Linkter
  • Live
  • MisterWong
  • MisterWong.DE
  • MSN Reporter
  • MySpace
  • PDF
  • Ping.fm
  • RSS
  • Slashdot
  • Socialogs
  • Technorati
  • Tumblr
  • Twitter
  • Twitthis
  • Webride
  • Yahoo! Bookmarks
  • Yigg
  1. Mar 14th, 2009 at 13:09
    Reply | Quote | #1

    main函数没有返回值,#include <iostream.h>应该是#include <iostream>[emot]01[/emot]

    • liduan
      Mar 14th, 2009 at 15:31
      Reply | Quote | #2

      如果不写exit(0)的话, c语言程序默认的返回值是0,当然编译的时候会有警告 。 unix下基本都是写成iostream.h,现在写成什么不重要了. [emot]02[/emot]

  2. haha
    Sep 27th, 2007 at 03:25
    Reply | Quote | #3

    [emot]01[/emot]复制下了,怎么没运行起了????

    • liduan
      Sep 27th, 2007 at 14:43
      Reply | Quote | #4

      >bcc32 -v- -w -O1 cpp.cpp
      Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
      cpp.cpp:
      Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
      >Exit code: 0
      >cpp
      enter the number of disks:3
      move top disk on tower A to tower B
      move top disk on tower A to tower C
      move top disk on tower B to tower C
      move top disk on tower A to tower B
      move top disk on tower C to tower A
      move top disk on tower C to tower B
      move top disk on tower A to tower B
      >Exit code: 0

      我的运行结果

Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).