Created
February 24, 2016 04:57
-
-
Save thmain/b5fecce295bd0c5a5f12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TOH { | |
public void Tower(int n, String src, String aux, String dest){ | |
if(n==1){ | |
System.out.println("Move disc "+ n + " from " + src + " to " + dest ); | |
} | |
else{ | |
//make recursive call to move n-1 disks to aux nail | |
Tower(n-1, src, dest, aux); | |
//move the nth disc from "A" to "B" | |
System.out.println("Move disc " + n + " from " + src + " to " + dest ); | |
//Move n-1 disc from aux mail to dest tail | |
Tower(n-1, aux, src, dest); | |
} | |
} | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
int n = 4; | |
TOH i = new TOH(); | |
//A, B, C -> A is source, B is destination and C is Auxiliary Nail | |
i.Tower(n,"A","C","B"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment