import java.util.Scanner; import java.util.*; public class Hanoi { @SuppressWarnings("unchecked") public static void main(String[] args) { System.out.println("How Many Disks?"); Scanner in = new Scanner(System.in); int n = in.nextInt(); Stack fullStack = new Stack<>(); for (int i = n; i > 0; i--) { fullStack.push(i); } Vertex start = new Vertex((Stack) fullStack.clone(), new Stack(), new Stack()); Vertex end = new Vertex(new Stack(), new Stack(), (Stack) fullStack.clone()); printSolution(start, end); } public static void printSolution(Vertex start, Vertex end) { List solutionString = bfs(start, end); Collections.reverse(solutionString); for (Vertex state : solutionString) { System.out.println(state); } System.out.println("Total Moves: " + (solutionString.size() - 1)); } public static List bfs(Vertex start, Vertex end) { Queue path = new LinkedList<>(); Set hasVisited = new HashSet<>(); List solution = new ArrayList<>(); Vertex temp = start; path.offer(temp); hasVisited.add(temp); while (!temp.equals(end) && !path.isEmpty()) { temp = path.poll(); for (Vertex n : temp.getChildren()) { if (!hasVisited.contains(n)) { path.offer(n); hasVisited.add(n); } } } for (Vertex vertex = temp; vertex != null; vertex = vertex.previous) { solution.add(vertex); } return solution; } }