Last active
August 29, 2015 14:15
-
-
Save dmnugent80/a300f5614f827860333e to your computer and use it in GitHub Desktop.
Tower of Hanoi
This file contains hidden or 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 MainClass { | |
public static void main(String[] args) { | |
int nDisks = 3; | |
doTowers(nDisks, 1, 2, 3); | |
} | |
public static void doTowers(int topN, int from, int inter, int 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); | |
} | |
} | |
} | |
/*output: | |
Disk 1 from 1 to 3 | |
Disk 2 from 1 to 2 | |
Disk 1 from 3 to 2 | |
Disk 3 from 1 to 3 | |
Disk 1 from 2 to 1 | |
Disk 2 from 2 to 3 | |
Disk 1 from 1 to 3 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment