문제
제출내용
import java.util.*;
public class Main {
static StringBuilder sb = new StringBuilder();
static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int inputNum = sc.nextInt();
HanoiTower(inputNum, 1, 2, 3);
System.out.println(count);
System.out.println(sb);
sc.close();
}
public static void HanoiTower(int inputNum, int from, int by, int to) {
count++;
if(inputNum==1) { // 원판이 1개일 경우
sb.append(from + " " + to + "\n");
return ;
}else { //원판이 1개가 아닐 경우
HanoiTower(inputNum-1, from, to ,by); // 1 -> 3 -> 2 순으로 원판 전달
sb.append(from + " " + to + "\n");
HanoiTower(inputNum-1, by, from, to); // 2 -> 1 -> 3 순으로 원판 전달
}
}
}
출처
백준 알고리즘