Created
July 3, 2014 21:20
-
-
Save jyhjuzi/b70ef4fa1bda7240fcfc to your computer and use it in GitHub Desktop.
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
import java.util.Stack; | |
public class Q3_4{ | |
public static void main(String[] args){ | |
Tower source = new Tower(new int[] {4,3,2,1},0); | |
Tower tool = new Tower(new int[0],1); | |
Tower des = new Tower(new int[0],2); | |
source.moveTo(des,tool,source.stack.size()); | |
} | |
} | |
class Tower{ | |
Stack<Integer> stack = new Stack<Integer>(); | |
int index; | |
Tower(int[] array, int i){ | |
for(int x : array){ | |
stack.push(x); | |
} | |
index = i; | |
} | |
void moveOne(Tower des){ | |
des.stack.push(stack.pop()); | |
System.out.println("move one from tower "+index + " to tower "+des.index); | |
} | |
void moveTo(Tower des, Tower tool, int size){ | |
if(size == 0) | |
return; | |
moveTo(tool, des, size -1); | |
moveOne(des); | |
tool.moveTo(des,this,size-1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment