Notice
Recent Posts
Recent Comments
05-18 00:26
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Byeol Lo

N11729 - 하노이 탑 이동 순서 본문

Algorithm/DevideConquer & Recursion

N11729 - 하노이 탑 이동 순서

알 수 없는 사용자 2023. 12. 11. 16:45
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.*;

public class Main {
    private static final BufferedWriter BW = new BufferedWriter(new OutputStreamWriter(System.out));
    private static final StringBuffer SB = new StringBuffer();
    private static Scanner SC = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        solution(SC.nextInt());
        BW.write(SB.toString());
        BW.close();
    }

    private static void solution(int n) {
        SB.append((int) Math.pow(2, n)-1).append("\n");
        hanoi(n, 1, 3);
    }

    private static void hanoi(int cnt, int move, int to) {
        if(cnt == 1) SB.append(move).append(" ").append(to).append("\n");
        else {
            hanoi(cnt-1, move, 6-move-to);
            hanoi(1, move, to);
            hanoi(cnt-1, 6-move-to, to);
        }
    }
}

'Algorithm > DevideConquer & Recursion' 카테고리의 다른 글

N4779 - 칸토어 집합  (0) 2023.12.11
N2447 - 별 찍기 - 10  (0) 2023.12.11
Comments