File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.math.BigInteger ;
4+
5+ public class Main {
6+ static int N ;
7+ static StringBuilder sb = new StringBuilder ();
8+
9+ public static void main (String [] args ) throws Exception {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
11+ N = Integer . parseInt(br. readLine());
12+
13+ BigInteger moves = BigInteger . valueOf(2 ). pow(N ). subtract(BigInteger . ONE );
14+ sb. append(moves). append(' \n ' );
15+
16+ if (N <= 20 ) {
17+ hanoi(N , 1 , 3 , 2 );
18+ }
19+
20+ System . out. print(sb. toString());
21+ }
22+
23+ // N개를 from -> to
24+ static void hanoi (int N , int from , int to , int temp ) {
25+ if (N == 0 ) return ;
26+ hanoi(N - 1 , from, temp, to); // N-1개를 출발 -> 보조 기둥으로 옮김
27+ sb. append(from). append(' ' ). append(to). append(' \n ' ); // 출발 -> 목적 기둥으로 남은 원판(젤 큰 거) 옮김
28+ hanoi(N - 1 , temp, to, from); // N-1개를 보조 -> 목적 기둥으로 옮김
29+ }
30+ }
31+ ```
You can’t perform that action at this time.
0 commit comments