需要解释我的河内塔递归代码的工作原理

编程入门 行业动态 更新时间:2024-10-22 15:43:03
本文介绍了需要解释我的河内塔递归代码的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚进入递归,我想我对它的工作原理有一个基本的了解.我有这个河内塔问题的代码,我已经盯着它看了一个小时,试图弄清楚它到底在做什么.'moveDisks' 方法让我感到困惑.我希望有人可以帮助解释该方法中发生的事情.不是我写的.

I am just getting into recursion and I think I have a basic understanding of how it works. I have this code for a Tower of Hanoi problem and I have been staring at it for an hour trying to figure out exactly what it is doing. The method 'moveDisks' is confusing to me. I was hoping someone could help explain what's going on in the method. I didn't write it.

为了尝试理解,我运行了代码并将磁盘数设为 2.这是打印出来的:

To try to understand I ran the code and put 2 for the number of disks. Here is what printed out:

动作是:将磁盘 1 从 A 移到 C,将磁盘 2 从 A 移到 B,将磁盘 1 从 C 移到 B

The moves are: Move disk 1 from A to C, Move disk 2 from A to B, Move disk 1 from C to B

所以,如果我理解正确的话,2 会将我们带到else"块,这意味着 2-1 将从 A 移动到 C.然后它再次减去 1 以进行另一次移动?我不明白接下来会发生什么或者为什么需要交替塔.

So, if I understand it right, the 2 brings us to the 'else' block which means that 2-1 is going to move from A to C. And then it subtracts 1 again to do another move? I don't understand what happens next or why the need to alternate towers.

import java.util.Scanner; public class TowersOfHanoi { /** Main method */ public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); System.out.print("Enter number of disks: "); int n = input.nextInt(); // Find the solution recursively System.out.println("The moves are:"); moveDisks(n, 'A', 'B', 'C'); } /** The method for finding the solution to move n disks from fromTower to toTower with auxTower */ public static void moveDisks(int n, char fromTower, char toTower, char auxTower) { if (n == 1) // Stopping condition System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); else { moveDisks(n - 1, fromTower, auxTower, toTower); System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); moveDisks(n - 1, auxTower, toTower, fromTower); } } }

推荐答案

递归是一种信仰的飞跃.重点是,您不需要尝试控制每一个小细节.您只是假设您已经编写了您的函数,并且它可以正常工作.然后你使用它.就这样.

Recursion is a leap of faith. The point to it is, you don't need to try to control every little detail. You just assume you've already written your function, and it works properly. Then you use it. That's all.

所以,它是反向归纳.

无论是塔楼,您都认为它已经可供您使用.因此,要将 n 个磁盘从 A 移动到 B,您将 (n-1) 个磁盘从 A 移动到 C.现在最大的磁盘仍然位于 A,并且整齐地在 C 处订购 (n-1) 个磁盘. B 是空的.只需将最后一个磁盘从 A 移到 B,然后将 (n-1) 个磁盘从 C 移到 B.大功告成.

Eith the Towers, you assume it's already at your disposal. So, to move n disks from A to B, you move (n-1) disks from A to C. You now have the biggest disk still at A, and neatly ordered (n-1) disks at C. The B is empty. Just move your one last disk from A to B, and move the (n-1) disks from C to B. You're done.

移动 n-1 个磁盘,即使 n 磁盘在周围也可以移动,因为所有 n-1 个磁盘都较小比 n,因此可以安全地忽略磁盘 n.任何 m、0 <米<n.

It's OK to move n-1 disks even when the disk n is lying around, because all the n-1 disks are smaller than n, and so the disk n can be safely ignored. Same goes for any m, 0 < m < n.

移动 0 个磁盘更容易,而且也不会违反任何法律.

Moving 0 disks is even easier, and doesn't break any laws either.

对于您的具体问题,

然后再减1再做一次?

不,n 还是一样的,所以 (n-1) 在两种情况下都是一样的.

no, the n is still the same, so (n-1) is the same in both cases.

你交替塔,最后降落在正确的塔上.

You alternate towers to land on the right one in the end.

更多推荐

需要解释我的河内塔递归代码的工作原理

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

发布评论

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

>www.elefans.com

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