河内塔递归java

编程入门 行业动态 更新时间:2024-10-23 16:26:36
本文介绍了河内塔递归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,

  • 从 移动 n-1 磁盘 (源塔) 其他 (中间塔)。
  • 然后移动 n 从 从 (源塔)到 到 (目标塔)的磁盘。
  • 最后从 其他 (中间塔)移动 n-1 磁盘) to (目的地塔)。
  • Move n-1 disks from "from" (source tower) to "other" (intermediary tower).
  • Then move n th disk from "from" (source tower) to "to" (destination tower).
  • 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);

    然后你基本上在做:

  • 将 n-1 磁盘从 从 (源塔)移至 其他 (中间塔)。
  • 然后移动 n-1 从 其他 (中间塔)到 到 (目标塔)的磁盘。
  • 最后从 从 移动 n 磁盘(来源)塔)到 到 (目的地塔)。

  • Move n-1 disks from "from" (source tower) to "other" (intermediary tower).
  • Then move n-1 disks from "other" (intermediary tower) to "to" (destination tower).
  • 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

    本文发布于:2023-11-30 03:09:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1648525.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:递归   河内   java

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!