-
-
Save JamesJi9277/99caad2644c621a98407 to your computer and use it in GitHub Desktop.
3.4.java
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
//3.4 Towers of Hanoi | |
public class Solution{ | |
public void moveDisks(String args[]){ | |
if(n < 1 ) | |
return; | |
int n =3; | |
Tower[] towers = new Tower[n]; | |
for(int i =0;i<n;i++) | |
towers[i] = new Tower(i); | |
for(int i < n;i>=0;i--) | |
towers[0].add(i); | |
towers[0].moveDisks(n,towers[2],towers[1]); | |
} | |
public class Tower{ | |
private Stack<Integer> disks; | |
private int index; | |
public Tower(int i ){ | |
return index; | |
} | |
public int index(){ | |
return index; | |
} | |
public void add(int d){ | |
if(!disks.isEmpty()&&disks.peek() <=d) | |
return; | |
else | |
disks.push(d); | |
} | |
public void moveTopTo(Tower t){ | |
int top = disks.pop(); | |
t.add(top); | |
} | |
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment