백준
#백준_11729 하노이 탑 이동 순서 - Java
ukyonge
2020. 1. 11. 20:01
# 하노이탑 알고리즘만 알면 바로 풀 수 있던 문제. 그러나 출력문이 많아 System.out.print를 사용하면 시간 초과 발생하니 유의할것
package bj;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class p11729 {
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
System.out.println((int)Math.pow(2, N)-1);
hanoi(N,1,2,3);
bw.close();
}
public static void hanoi(int N, int a, int b, int c) throws IOException {
if(N>0) {
hanoi(N-1, a, c, b);
bw.write(a+" "+c+"\n");
hanoi(N-1, b, a, c);
}
}
}