Last active
August 29, 2015 14:03
-
-
Save chrislukkk/9e479f22da6165f83b9d to your computer and use it in GitHub Desktop.
CareerCup_3.4 - HanoiTower
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
package Chapter3; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class HanoiTower { | |
private List<Stack<Integer>> towers; | |
public HanoiTower(int t, int d) { | |
towers = new ArrayList<>(); | |
for (int i = 0; i < t; i++) { | |
towers.add(new Stack<Integer>()); | |
} | |
for (int i = d; i > 0; i--) { | |
towers.get(0).push(i); | |
} | |
} | |
// move the tower from start to end | |
public void finishHanoi() { | |
move(towers.get(0).size(), 0, towers.size() - 1); | |
} | |
// moving n disks from one tower to another | |
private void move(int n, int from, int to) { | |
if (n == 1) { | |
// base case - moving one disk | |
slide(from, to); | |
return; | |
} | |
int another = (from + to) == 1 ? 2 : ((from + to) == 2 ? 1 : 0); | |
move(n - 1, from, another); | |
slide(from, to); | |
move(n - 1, another, to); | |
} | |
// sliding one disk from one tower to another | |
private void slide(int from, int to) { | |
if (towers.get(to).isEmpty() | |
|| towers.get(from).peek() < towers.get(to).peek()) { | |
int diskSize = towers.get(from).pop(); | |
towers.get(to).push(diskSize); | |
System.out.println("move disk " + diskSize + " from Tower " | |
+ (from + 1) + " to Tower " + (to + 1)); | |
} else | |
System.out.println("moving large disk on top of small disk"); | |
} | |
private void print() { | |
int i = 1; | |
for (Stack<Integer> t : towers) { | |
System.out.print("[Tower " + (i++) + "]: "); | |
t.print(); | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
HanoiTower tower = new HanoiTower(3, 5); | |
System.out.println("----------------------------------------------"); | |
tower.print(); | |
System.out.println("----------------------------------------------"); | |
tower.finishHanoi(); | |
System.out.println("----------------------------------------------"); | |
tower.print(); | |
System.out.println("----------------------------------------------"); | |
} | |
} |
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
package Chapter3; | |
public class Node<T> { | |
public T value; | |
public Node<T> next; | |
public Node(T d, Node<T> n){ | |
value = d; | |
next = n; | |
} | |
} |
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
package Chapter3; | |
public class Stack<T> { | |
private int size = 0; | |
public Node<T> top = null; | |
public int size() { | |
return size; | |
} | |
public boolean isEmpty() { | |
return size == 0; | |
} | |
public void push(T value) { | |
Node<T> n = new Node<T>(value, top); | |
size++; | |
top = n; | |
} | |
public T pop() { | |
if (top == null) | |
return null; | |
T res = top.value; | |
top = top.next; | |
size--; | |
return res; | |
} | |
public void print() { | |
if(top == null) return; | |
Node<T> cur = top; | |
while(cur != null) { | |
System.out.print(cur.value + "; "); | |
cur = cur.next; | |
} | |
System.out.println(); | |
} | |
public T peek() { | |
if (top == null) | |
return null; | |
return top.value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment