zoukankan
html css js c++ java
睡不着,随便写了下汉诺塔的解决方法。
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(
string
[] args)
{
int
times
=
new
HanoiTower(
3
).SolveTower();
Console.WriteLine(
"
一共需要{0}次
"
, times);
Console.ReadKey();
}
}
class
HanoiTower
{
private
int
plates;
public
HanoiTower(
int
plates)
{
this
.plates
=
plates;
}
public
int
SolveTower()
{
return
SolveTower(plates,
"
A
"
,
"
B
"
,
"
C
"
);
}
private
int
SolveTower(
int
plates,
string
a,
string
b,
string
c)
{
int
count
=
0
;
if
(plates
<=
0
)
throw
new
ArgumentOutOfRangeException(
"
plates
"
,
"
盘子数必须是大于0的整数
"
);
if
(plates
==
1
)
{
move(a, c);
return
1
;
}
count
+=
SolveTower(plates
-
1
, a, c, b);
count
+=
SolveTower(
1
, a, b, c);
count
+=
SolveTower(plates
-
1
, b, a, c);
return
count;
}
private
void
move(
string
a,
string
c)
{
Console.WriteLine(a
+
"
=>
"
+
c);
}
}
}
张旋(zxsoft)
如对本文有什么疑问,请在下面写下留言,谢谢!
查看全文
相关阅读:
hdu2604 矩阵快速幂
自己对有上下界的网络流的理解
自己对有上下界的网络流的理解
POJ 2396 构造矩阵(上下流)
POJ 2396 构造矩阵(上下流)
hdu4940 有上下界的无源可行流判断
hdu4940 有上下界的无源可行流判断
hdu4515 小模拟
hdu4515 小模拟
hdu4901 枚举状态(找集合对S(xor) ==T(and))
原文地址:https://www.cnblogs.com/zxsoft/p/1210400.html
最新文章
【CF468B】Two Sets
【GMOJ4488】疯狂动物城
【2020牛客NOIP赛前集训营-普及组】飞行棋
【CF786E】ALT
MyEclipse配置输出控制台信息至文本文件中
UVA10106
UVA424
MYSQL服务无法启动,提示信息1067解决办法
iOS开发中很重要,很常用,但却容易被忽略的知识点:id ,NSObject, id<NSObject>区别
hdu 2087
热门文章
利用ffmpeg做视频解码的顺序
解决mysql汉字存储问题
C++文件输入输出小例子
mysql入门
hdu4965 巧用矩阵乘法结合律
hdu3117 斐波那契前后4位
hdu3117 斐波那契前后4位
hdu1568斐波那契前4位
hdu1568斐波那契前4位
hdu2604 矩阵快速幂
Copyright © 2011-2022 走看看