-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHanoi.java
More file actions
69 lines (57 loc) · 1.73 KB
/
Hanoi.java
File metadata and controls
69 lines (57 loc) · 1.73 KB
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
package edu.java.chap3;
import java.util.Stack;
/* In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide on to any
tower The puzzle starts with disks sorted in ascending order of size from top to bottom (e g , each disk sits on top of an
even larger one) You have the following constraints:
(A) Only one disk can be moved at a time
(B) A disk is slid off the top of one rod onto the next rod
(C) A disk can only be placed on top of a larger disk
Write a program to move the disks from the first rod to the last using Stacks
*/
public class Hanoi {
/**
* @param args
*/
public static void main(String[] args) {
int n = 5;
Tower[] towers = new Tower[n];
for (int i = 0; i < 3; i++) towers[i] = new Tower(i);
for (int i = n - 1; i >= 0; i--) towers[0].add(i);
towers[0].moveDisks(n, towers[2], towers[1]);
}
}
class Tower {
private Stack<Integer> disks;
private int index;
public Tower(int i ){
this.disks = new Stack<Integer>();
this.index = i;
}
public int index(){
return index;
}
public void add(int d){
if (!disks.isEmpty() && disks.peek()<d){
System.out.println("Cannot put on top of it");
} else {
disks.push(d);
}
}
public void moveTopTo(Tower t){
int top = disks.pop();
t.add(top);
System.out.println("Move"+ top + "from "+this.index()+ "to"+ t.index());
}
public void print() {
System.out.println("tower print+ "+this.index());
for (int i = disks.size() - 1; i >= 0; i--) {
System.out.println(" " + disks.get(i)); }
}
public void moveDisks(int n, Tower destination, Tower buffer) {
if (n > 0) {
moveDisks(n - 1, buffer, destination);
moveTopTo(destination);
buffer.moveDisks(n - 1, destination, this);
}
}
}