Hanoi Tower: 经典的BFS(宽度广度优先搜索)再演绎

TopCoder SRM 446 1000-Point Problem - HanoiTower

Problem Statement

    

In this problem we consider a modification of the classical Hanoi Towers game. The rules of our modification are as follows:

  • There are three pegs: peg A, peg B and peg C.
  • Each peg initially contains zero or more discs.
  • All discs have the same size and there are three types of discs: type A, type B and type C.
  • In one move you can move a disc from the top of one peg onto the top of another peg.
  • Your goal is to have all discs of type A on peg A, all discs of type B on peg B and all discs of type C on peg C.
  • You must achieve your goal in the minimum possible number of moves.

The initial locations of the discs are given in the strings pegA, pegB and pegC. Each character of pegA represents a disc located on peg A, where 'A' represents a disc of type A, 'B' represents a disc of type B and 'C' represents a disc of type C. The discs are given in order, from bottom to top. pegB and pegC describe the discs on pegs B and C, respectively, in the same format.

Return the minimum number of moves required to achieve the goal of the game.

Definition

    
Class:HanoiTower
Method:moves
Parameters:string, string, string
Returns:int
Method signature:int moves(string pegA, string pegB, string pegC)
(be sure your method is public)
    
 

Notes

-It's always possible to achieve the goal of the game.

Constraints

-pegA, pegB and pegC will contain only the characters 'A', 'B' and 'C'.
-The total number of characters in pegA, pegB and pegC will be between 1 and 10, inclusive.

Examples

0) 
    
"A"
"AA"
"AA"
Returns: 4
Move all discs of type A to peg A directly.
1) 
    
"B"
"C"
"A"
Returns: 5
There is exactly one disc of each type in this example, so we will refer to each disc by its type. The following sequence of moves is the shortest:
1. Move disc A to peg A.
2. Move disc C to peg C.
3. Move disc A to peg C.
4. Move disc B to peg B.
5. Move disc A to peg A.
2) 
    
"CBA"
""
""
Returns: 5
Again, there is exactly one disc of each type here, so we will refer to each disc by its type. The following sequence of moves is the shortest:
1. Move disc A to peg C.
2. Move disc B to peg B.
3. Move disc A to peg B.
4. Move disc C to peg C.
5. Move disc A to peg A.
3) 
    
"BBBBBBBBBA"
""
""
Returns: 11
Move the disc of type A to peg C. Then move all discs of type B to peg B. Finally, move disc A back to peg A.
4) 
    
"CBACBACBAA"
""
""
Returns: 19
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

 

汉诺塔问题,是一个经典的递归算法案例。这道题继承了汉诺塔的特点,同时特殊的修改使其比普通的汉诺塔问题更具有挑战性。递归问题大多可用动态规划结合BFS(广度优先搜索,又称宽度优先搜索)的算法,这道题也一样,解答步骤为:

1、对于每一个状态(pegA, pegB, pegC),由于要寻找其解,放入当前步骤(搜索层)问题表(队列堆栈皆可)。最开始初始化时表中只有一个元素,即最初状态。(这里看到有些选手采用优先级队列,那样的话STL底层维护起来很麻烦,其实只要用BFS就没有必要用优先级队列)

2、维护一个搜索集set。对于状态(pegA, pegB, pegC),如果已在搜索中,则该分支陷入重复,放弃该分支搜索;否则加入搜索集。(一般set无需判断,直接insert即可)

3、遍历当前问题表求解。如果有某一状态满足终止条件,那么当前步骤数即为所求,返回;如果没有,则展开得到更深层次的问题表,步骤数++。返回步骤1。

注意:建议对“状态(pegA, pegB, pegC)”进行优化,最好将其计算为一个数字。如本题可以采用一个“三进制数”表达一个peg。看到有些选手直接用string[3]来代表这个状态,复杂度比较高,不过TC系统测试也能通过。

由于比赛完已经很晚了,稍微描述了一下思路,希望能对感兴趣的朋友一些参考和帮助。也欢迎你们留言发表自己的看法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
四柱汉诺塔是汉诺塔问题的一个变种,与经典的三柱汉诺塔问题不同,它有四个柱子而不是三个。规则如下: 1. 有四个柱子,标记为 A、B、C 和 D。 2. 一开始时,所有的盘子都在柱子 A 上,从大到小排列。 3. 目标是将所有盘子从柱子 A 移动到柱子 D 上,保持它们的顺序不变。 4. 在移动盘子时,可以执行以下操作之一: a. 将一个盘子从一个柱子移动到另一个柱子。 b. 将一个盘子从一个柱子移动到另一个柱子上的另一个盘子上,使其成为该柱子上的顶部盘子。 5. 在任何时候,不能将较大的盘子放在较小的盘子上面。 要解决四柱汉诺塔问题,可以使用递归算法,将问题分解为多个子问题,然后再逐步解决这些子问题。具体方法如下: 1. 如果要移动的盘子数目为 1,则将它直接从柱子 A 移动到柱子 D。 2. 如果要移动的盘子数目为 2,则可以先将一个盘子从柱子 A 移动到柱子 B,再将另一个盘子从柱子 A 移动到柱子 D,最后将柱子 B 上的盘子移动到柱子 D 上。 3. 如果要移动的盘子数目大于 2,则可以将它们分为两部分,其中一部分包括最上面的盘子,另一部分包括剩余的盘子。将第一部分的盘子从柱子 A 移动到柱子 B,然后将剩余的盘子从柱子 A 移动到柱子 C。接下来,将柱子 B 上的盘子和柱子 C 上的盘子移动到柱子 D 上。最后,将柱子 C 上的盘子移动到柱子 B 上,然后将柱子 D 上的盘子移动到柱子 C 上。现在,所有盘子都在柱子 B 和柱子 C 上,可以使用递归算法将它们移动到柱子 D 上。 下面是一个用 Python 语言实现的示例代码: ``` def four_column_hanoi(n, A, B, C, D): if n == 1: print(f"Move disk {n} from {A} to {D}") elif n == 2: print(f"Move disk {1} from {A} to {B}") print(f"Move disk {2} from {A} to {D}") print(f"Move disk {1} 我不熟悉汉诺塔。四柱汉诺塔是汉诺塔问题的一个变体,需要将一堆不同大小的圆盘从一个起始柱子移动到目标柱子,但是这次有四个柱子可用于移动圆盘,而不是原来的三个。 要解决四柱汉诺塔问题,可以采用递归的方法。下面是一个简单的解法: 1. 如果圆盘数量为 0,则返回。 2. 将所有小于等于当前圆盘的圆盘移动到辅助柱子 A。 3. 将当前圆盘移动到目标柱子 D。 4. 将辅助柱子 A 上的所有圆盘移动到辅助柱子 B。 5. 将所有小于等于当前圆盘的圆盘移动到目标柱子 D。 6. 将辅助柱子 B 上的所有圆盘移动到辅助柱子 A。 7. 重复步骤 3 到 6,直到所有圆盘都移动到目标柱子 D。 以下是一个 Python 代码示例: ``` def four_column_hanoi_tower(n, start, end, aux1, aux2): if n == 0: return if n == 1: print(f"Move disk 1 from {start} to {end}") return four_column_hanoi_tower(n-1, start, aux1, aux2, end) print(f"Move disk {n} from {start} to {end}") four_column_hanoi_tower(n-1, aux1, end, aux2, start) print(f"Move disk {n} from {end} to {start}") four_column_hanoi_tower(n-1, aux2, aux1, end, start) print(f"Move disk {n} from {start} to {end}") four_column_hanoi_tower(n-1, aux1, end, aux2, start) ``` 其中,n 表示圆盘的数量,start、end、aux1 和 aux2 分别表示起始柱子、目标柱子和两个辅助柱子的名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值