-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTowersOfHanoi.java
More file actions
33 lines (28 loc) · 928 Bytes
/
TowersOfHanoi.java
File metadata and controls
33 lines (28 loc) · 928 Bytes
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
// Ahmad Barhamje & Brett Fazio, Towers of Hanoi
public class TowersOfHanoi {
public static void main(String[] args) {
//takes 2^n - 1 moves to solve a tower of hanoi with n disks.
solve(3, 'A', 'B', 'C');
}
static void solve(int count, char s, char d, char i) {
if(count == 1) {
System.out.printf("Move top disc from pole %c to pole %c\n", s, d);
} else {
solve(count-1, s, i, d);
solve(1, s, d, i);
solve(count-1, i, d, s);
}
}
// formula is for a towers of hanoi with THREE towers (IE Normal)
// s = stack number (0 = left, 1 = mid, 2 = right)
// m = move number (0 = initial state)
// d = disk number (1 = topmost disk)
// n = total number of disks
static int stateCalculation(int s, int m, int d, int n) {
int adder = (n+d+1)%2;
adder++;
adder *= (int)Math.floor((m+Math.pow(2, d-1)/Math.pow(2, d)));
adder = adder % 3;
return adder;
}
}