Last active
July 26, 2016 04:50
-
-
Save flying3615/9d466894c3fab1f5c390c0705ce57629 to your computer and use it in GitHub Desktop.
recursive solution for HanoiTower
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
package recursion; | |
import java.util.Scanner; | |
/** | |
* | |
The base case for the problem is n = 1. If n == 1, you could simply move the disk from A | |
to B. When n > 1, you could split the original problem into the following three subproblems | |
and solve them sequentially. | |
1. Move the first n - 1 disks from A to C recursively with the assistance of tower B, | |
2. Move disk n from A to B, | |
3. Move n - 1 disks from C to B recursively with the assistance of tower A | |
* @author yufliu21 | |
* | |
*/ | |
public class TowerOfHanoi { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Enter number of disks: "); | |
int n = scanner.nextInt(); | |
System.out.println("The moves are: "); | |
moveDisk(n,"A","B","C"); | |
} | |
private static void moveDisk(int n, String fromTower, String toTower, String auxTower) { | |
//If n == 1, you could simply move the disk from A to B | |
if(n==1){ | |
System.out.println("Move disk "+n+" from "+fromTower+" to "+toTower); | |
}else { | |
//Move the first n - 1 disks from A to C recursively with the assistance of tower B | |
moveDisk(n-1, fromTower, auxTower, toTower); | |
//Move disk n from A to B | |
System.out.println("Move disk "+n+" from "+fromTower+" to "+toTower); | |
//Move n - 1 disks from C to B recursively with the assistance of tower A | |
moveDisk(n-1, auxTower, toTower, fromTower); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment