1 min to read
하노이탑
하노이탑

import java.math.BigInteger;
import java.util.Scanner;
public class Main{
static void hanoi(int N,char from,char aux,char to) {
if(N==1) {
System.out.println(from+" "+to);
}else {
hanoi(N-1,from,to,aux); //n-1개의 원반을 보조탑(2번탑으로 옮기는과정)으로 옮기는 과정
System.out.println(from+" "+to); //가장 바닥에 있는 원반을 1번탑에서 3번탑으로 옮기는 과정
hanoi(N-1,aux,from,to); //보조탑에 있던 n-1개의 원반을 3번탑으로 옮기는 과정
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
BigInteger result=new BigInteger("1");
if(N==1) {
System.out.println(1);
}
else {
for(int i=0;i<N;i++) {
result=result.multiply(new BigInteger("2"));
}
result=result.subtract(new BigInteger("1"));
System.out.println(result);
}
if(N<=20) {
hanoi(N,'1','2','3');
}
}
}
Comments