Java implements Tower of Hanoi

Reprinted from: http://blog.csdn.net/ljmingcom304/article/details/50296939

There are 3 stone pillars A, B, and C of the same size. Among them, pillar A has n plates placed in order from bottom to top. Now it is necessary to move all the plates of stone pillar A to stone pillar C, and each time only A disc can be moved, but a small disc cannot be placed on a large disc. How can I move it?
write picture description here

算法分析:
When n=1, that is, only one disk is placed on the stone pillar A at the beginning, then the disk can be directly moved from the stone pillar A to the stone pillar B.
  When n=2, the discs are numbered as No. 1 and No. 2 in order of size from top to bottom, then to move all the discs from Pillar A to Pillar C, firstly, the No. 1 disk needs to be moved to Pillar B, Then move the No. 2 disk to the stone pillar C, and finally move the No. 1 disk to the stone pillar C.
  When n=3, the disks are still numbered as No. 1, No. 2 and No. 3 in the order of size from top to bottom. At this time, because the problem is relatively complicated, the No. 1 and No. 2 disks are regarded as one disk, that is For discs 1+2, what needs to be solved at this time is the problem of moving discs 1+2 and 3 to stone pillar C, that is, first move disks 1+2 to stone pillar B, and then move No. 3 disks to stone pillar B. Move the disk to Pillar C, and finally move the No. 1+2 disk to Pillar C.
  Since only one disc can be moved at a time, if you want to move the No. 1+2 disc to the stone pillar B, you need to split the No. 1+2 disc into two individuals, which is regarded as the No. 1 and No. 2 discs. Move to Pillar B, and move the No. 1+2 disk to Pillar C in the same way.
  And so on...
  At that timen=n , the discs were numbered from top to bottom as No. 1, No. 2, No. 3... No. n. Similarly, the discs from No. 1 to n-1No. 1 were regarded as one disc, namely ∑n1(n−1)No. At this time, the solution is to move the ∑n1(n−1)numbered disk and the n-numbered disk to the stone pillar C, that is, first ∑n1(n−1)move the numbered disk to the stone pillar B, then move the n-numbered disk to the stone pillar C, and finally move the ∑n1(n−1)numbered disk to the stone pillar C. stone pillar C. Because after moving the nth disk to the stone pillar C, no matter how the first n-1 disks move, there is no need to move the nth disk again, that is, the parent problem and the child problem are relatively independent and do not affect each other, so it can be ∑n1(n−1)Divide and move down the problem of the number disc in the same way.

n=1时
1st disc, move from A to C
n=2时
1st disc, move from A to B
2nd disc, move from A to C
1st disc, move from B to C
n=3时
1st disc, Move from A to C
2nd disc, move from A to B
1st disc, move from C to B
3rd disc, move from A to C
1st disc, move from B to
A 2 discs, move from B to C
1st disc, move from A to C

To summarize:
n plates and 2 plates or 3 plates solve the problem in the same form.
Move n-1 plates to column B in an orderly manner. Move
n plate to C and
n-1 plates. Move from column B to C (this step is actually the same as moving n-1 plates from column A to C to solve the problem)

Let's say 10 plates are moved from A to C.
According to the divide and conquer idea, we 分解问题。
move 10 disks from A to C = 9 disks from A to B, 1 disk from A to C, 9 disks from B to C
= 8 disks from A to C , 1 plate is moved from A to B, 8 plates are moved from A to B, 1 plate is moved from A to C,
8 plates are moved from B to A, 1 plate is moved from B to C, 8 plates are moved from A moves to C

注意:
10 plates moved from A to C
9 plates moved from A to B
8 plates moved from A to C
and so on these processes split the big problem until the split moves from 1 plate from one column to another
Fully in line with the characteristics of the divide and conquer strategy.

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author wangzha
 * @see [相关类/方法](可选)
 * @since [产品/模块版本] (可选)
 */
public class HanoiTower {
    public static void hanoi(int n, String a, String b,String c) {
        if (n == 1) {
            // 只有一个圆盘时直接从A石柱移动到C石柱
            move(n, a, c);
        } else {
            // 将前n-1个圆盘从石柱A移动到石柱B
            hanoi(n - 1, a, c, b);
            // 将第n号圆盘从石柱A移动到石柱C
            move(n, a, c);
            // 将前n-1个圆盘从石柱B移动到石柱C
            hanoi(n - 1, b, a, c);
        }
    }

    public static void move(int n, String i, String j) {
        System.out.println("第" + n + "个圆盘," + "从" + i + "移动到" + j);
    }

    public static void main(String[] args) {
        hanoi(2,"A","B","C");
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340041&siteId=291194637