Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class run{
- public static void main(String[] args) throws FileNotFoundException,IOException{
- //String to file
- String file = "Prob15.in.txt";
- //New BufferedReader object
- BufferedReader br = new BufferedReader(new FileReader(file));
- //Acquire test cases
- int testCases = Integer.parseInt(br.readLine());
- //Loop through test cases
- while(testCases-- > 0){
- //Read disk amount
- int n = Integer.parseInt(br.readLine());
- //Print out amount of disks in stack
- System.out.println(n);
- //Solve towers
- solveTowers(n, "A", "B", "C");
- }
- }
- public static void printMove(String from, String to){
- System.out.println(from + "->" + to);
- }
- public static void solveTowers(int n, String from, String spare, String to){
- if(n == 1){
- printMove(from, to);
- }else{
- solveTowers(n - 1, from, to, spare);
- printMove(from, to);
- solveTowers(n - 1, spare, from, to);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement