-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTowerOfHanoiDemo.java
138 lines (113 loc) · 2.83 KB
/
TowerOfHanoiDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ds_008_recursion;
/*
* O(2^N)
*/
public class TowerOfHanoiDemo {
public static void main(String[] args) {
final int N = 3;
final Integer[][] pegs = initPegs(N);
printPegs(pegs);
System.out.println();
solve(pegs);
printPegs(pegs);
}
private static void solve(Integer[][] pegs) {
int srcPeg = 0;
int dstPeg = 2;
int numberOfDisks = pegs[0][0];
move(pegs, srcPeg, dstPeg, numberOfDisks);
}
private static void move(Integer[][] pegs, int srcPeg, int dstPeg, int numOfDisks) {
if(numOfDisks == 0) {
return;
}
// which peg will be used temporary peg?
int tmpPeg = getTemporaryPeg(srcPeg, dstPeg);
// move upper part from src to temp
move(pegs, srcPeg, tmpPeg, numOfDisks-1);
// move biggest disk to dst
moveOneDisk(pegs, srcPeg, dstPeg);
// printPegs(pegs);
// move from temp to dst
move(pegs, tmpPeg, dstPeg, numOfDisks-1);
}
private static void moveOneDisk(Integer[][] pegs, int srcPeg, int dstPeg) {
int moveIndex = getTopDiskIndex(pegs, srcPeg);
int emptyIndex = getNextEmptyIndex(pegs, dstPeg);
Integer moved = pegs[srcPeg][moveIndex]; // store the disk
pegs[srcPeg][moveIndex] = null; // disk left the src peg
pegs[dstPeg][emptyIndex] = moved;
}
private static int getNextEmptyIndex(Integer[][]pegs, int peg) {
for(int j = 0; j < pegs[peg].length; j++) {
if(pegs[peg][j] == null) {
return j;
}
}
return -1;
}
private static int getTopDiskIndex(Integer[][]pegs, int peg) {
for(int j = pegs[peg].length-1; j >= 0; j--) {
if(pegs[peg][j] != null) {
return j;
}
}
return -1;
}
private static int getTemporaryPeg(int srcPeg, int dstPeg) {
int newPeg;
if(srcPeg == 0) {
if(dstPeg == 1) {
newPeg = 2;
} else {
// dstPeg == 2
newPeg = 1;
}
} else if(srcPeg == 1) {
if(dstPeg == 0) {
newPeg = 2;
} else {
// dstPeg == 2
newPeg = 0;
}
} else {
// srcPeg == 2
if(dstPeg == 1) {
newPeg = 0;
} else {
// dstPeg == 0
newPeg = 1;
}
}
return newPeg;
}
private static final Integer[][] initPegs(final int N) {
final Integer[][] pegs = new Integer[3][N];
for(int width = N, index = 0; width > 0; width--, index++) {
pegs[0][index] = width;
}
return pegs;
}
private static void printPegs(final Integer[][] pegs) {
for(int i = 0; i < pegs.length; i++) {
System.out.print("|: ");
for(int j = 0; j < pegs[i].length ; j++) {
if(pegs[i][j] != null) {
System.out.printf("%d ", pegs[i][j]);
}
}
System.out.print("\n");
}
System.out.print("\n");
}
private static void printPegsVerbose(final Integer[][] pegs) {
for(int i = 0; i < pegs.length; i++) {
System.out.print("|: ");
for(int j = 0; j < pegs[i].length ; j++) {
System.out.printf("%d ", pegs[i][j]);
}
System.out.print("\n");
}
System.out.print("\n");
}
}