河内塔递归java [英] Tower of Hanoi recursion java

查看:92
本文介绍了河内塔递归java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用递归解决河内塔的Java代码:

Here's my Java code for solving Tower of Hanoi using recursion:

/**here is a stack of N disks on the first of three poles (call
them A, B and C) and your job is to move the disks from pole A to pole B without
ever putting a larger disk on top of a smaller disk.*/ 

public class Hanoi {

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

    //move n disks from position "from" to "to" via "other"
    private static void playHanoi(int n, String from , String other, String to) {
        if (n == 0)
            return;
        if (n > 0)
        playHanoi(n-1, from, to, other);
        System.out.printf("Move one disk from pole %s to pole %s \n ", from, to);
        playHanoi(n-1, other, from, to);
    }

}

我放置打印的地方吗方法有关系?另外,我可以这样做:

Does the place I put the print method matter? Also, can I do it like this:

    playHanoi(n-1, from, to, other);
    playHanoi(n-1, other, from, to);
    System.out.printf("Move one disk from pole %s to pole %s \n ", from, to);


推荐答案

解决 Hanoy之塔这样的问题,只不过是定义了你想要完成工作的策略。和你的代码:

Solving Tower of Hanoy problem in this way, is nothing but defining the strategy of how you want to get the job done. And your code :

    playHanoi(n-1, from, to, other);
    System.out.printf("Move one disk from pole %s to pole %s \n ", from, to);
    playHanoi(n-1, other, from, to);

基本上将您的策略​​定义为吼叫,

Basically defines your strategy to like bellow,


  1. 移动 n-1 磁盘 (源塔) 其他 (中间塔)。

  2. 然后移动 n (源塔)到 (目标塔)的磁盘。

  3. 最后从 其他 (中间塔)移动 n-1 磁盘) to (目的地塔)。

  1. Move n-1 disks from "from" (source tower) to "other" (intermediary tower).
  2. Then move n th disk from "from" (source tower) to "to" (destination tower).
  3. Finally move n-1 disks from "other" (intermediary tower) to "to" (destination tower).

您的 prinf 基本上是 2 nd步骤。

Your prinf basically does the 2 nd step.

现在,如果你写这样的代码:

Now if you write code like this:

    playHanoi(n-1, from, to, other);
    playHanoi(n-1, other, from, to);
    System.out.printf("Move one disk from pole %s to pole %s \n ", from, to);

然后你基本上在做:


  1. n-1 磁盘从 (源塔)移至 其他 (中间塔)。

  2. 然后移动 n-1 其他 (中间塔)到 (目标塔)的磁盘。

  3. 最后从 移动 n 磁盘(来源)塔)到 (目的地塔)。

  1. Move n-1 disks from "from" (source tower) to "other" (intermediary tower).
  2. Then move n-1 disks from "other" (intermediary tower) to "to" (destination tower).
  3. Finally move n th disk from "from" (source tower) to "to" (destination tower).


在此策略中,执行 2 步骤后(从 移动所有 n-1 磁盘)其他 ), 3 rd步骤无效(移动 n 磁盘从 )!因为在 Tower of Hanoy 中你不能把较大的磁盘放在较小的磁盘上!

In this strategy, after doing the 2 nd step (moving all n-1 disks from "other" to "to"), 3 rd step becomes invalid(moving n th disk from "from" to "to")! Because in Tower of Hanoy you cant put a larger disk on a smaller one!


因此,选择第二个选项(策略)会导致您采用无效策略,这就是为什么您不能这样做的原因!

So choosing the second option(strategy) leads you to an invalid strategy, thats why you can not do that!

这篇关于河内塔递归java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆