Created
June 28, 2012 11:45
-
-
Save ffbit/3010872 to your computer and use it in GitHub Desktop.
Tower of Hanoi problem.
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 TowerofHanoi | |
{ | |
public static void main(String...args) { | |
int nDisks = 3; | |
doTowers(nDisks, 'A', 'B', 'C'); | |
} | |
private static void doTowers(int topN, char from, char inter, char to) | |
{ | |
if (topN == 1) { | |
System.out.println("Disk 1 from " + from + " to " + to); | |
} else { | |
doTowers(topN - 1, from, to, inter); | |
System.out.println("Disk " + topN + " from " + from + " to " + to); | |
doTowers(topN - 1, inter, from, to); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment