数据结构学习(2):汉诺塔问题

None.gif using  System;
None.gif
None.gif
namespace  Hanoi
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            moveTowers(
3/**//*disks*/,/**//*from peg*/1,/**//*to peg*/3,/**//*using spare peg*/2);
InBlock.gif          Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void moveTowers(int n,int start,int finish,int spare)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//利用作为辅助的备用柱子将位于起始
InBlock.gif            
//柱子的n个圆盘移到终止柱子上
InBlock.gif
            if(n==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"move a disk from peg"+start+"to peg"+finish);
ExpandedSubBlockEnd.gif                }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                moveTowers(n
-1,start,spare,finish);
InBlock.gif                Console.WriteLine(
"move a disk from peg "+start+"to peg"+finish);
InBlock.gif                moveTowers(n
-1,spare,finish,start);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/tyrael007/archive/2006/12/14/592453.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值