Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tower of Hanoi recursion java

Tags:

java

recursion

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);
like image 437
user2372074 Avatar asked May 11 '13 03:05

user2372074


2 Answers

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

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);

Then you are basically doing:

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

    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!

like image 184
Sazzadur Rahaman Avatar answered Oct 04 '22 02:10

Sazzadur Rahaman


It does indeed matter. Anything after your recursion call will be executed after that recursion unwinds (and anything before it, before), so you might find your output is in a nonsensical order.

Keep in mind that the statement after the function call doesn't execute until the function returns.

like image 39
Falcon Momot Avatar answered Oct 04 '22 02:10

Falcon Momot