## Hanoi Tower: 经典的BFS（宽度广度优先搜索）再演绎

### 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.

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

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

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

posted on 2009-08-13 14:38  CLive Studio  阅读(...)  评论(...编辑  收藏

• 随笔 - 23
• 文章 - 0
• 评论 - 33
• 引用 - 0