C#递归实现汉诺塔(Hanoi) 游戏解法
汉诺塔简介 上帝创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上安大小顺序摞着64片黄金圆盘。 上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并
汉诺塔简介
上帝创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上安大小顺序摞着64片黄金圆盘。
上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。
有预言说,这件事完成时宇宙会在一瞬间闪电式毁灭。也有人相信婆罗门至今还在一刻不停地搬动着圆盘。
下面使用递归实现

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HanoiProgram { class Program { static long count = 0; static void move(char x,char y) { Console.WriteLine("{0}--->{1}",x,y); } static void hanoi(int n,char one,char two,char three) { if (n == 1) { count =2; move(one, two); move(two, three); } else { count = 2; hanoi(n - 1, one,two,three); move(one, two); hanoi(n - 1, three,two,one); move(two,three); hanoi(n - 1, one, two, three); } } static void Main(string[] args) { Console.WriteLine("需要搬几个盘子"); int n =int.Parse(Console.ReadLine()); hanoi(n, 'A', 'B', 'C'); Console.WriteLine("搬迁结速;一共进行了{0}次的搬迁。", count); Console.Read(); } } }
收藏文章
精彩图集
精彩文章
- 暂无记录。
热门标签
services
之二
sheet
u
spli
select刷新
BigDecimal类
hashlib模块
两位
getResponseC
符号
改变
php 一个简单
大型
linux开发经验
CCUserDefaul
透明窗体
点阵状
myanmar
Federated
getHeader
存储过程加密
vsftpd
nowdoc
浏览
垂直导航
基本类型
jquery遍历chec
renderPartia
strtr
val
Hacking
求n次方
图像方向
以斜杠结尾
gravity
图片滚动
游标
图片缩放
SQL触发器
database
多选下拉框
自动排序
资源调控器
CHTSli
格式化
kaptcha
0x80070005
图形
正版
写日志
引起
延时处
定时关机
链式二叉树
压缩样式表 p
python元组操
内连接
vmtools
ReflectionCl
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229