곽로그

[백준 11729] 하노이의 탑 본문

알고리즘/백준

[백준 11729] 하노이의 탑

일도이동 2020. 2. 26. 00:08
반응형
import java.io.BufferedWriter;
import java.util.Scanner;

public class Main {
	public static StringBuffer sb =new StringBuffer();
	public static int count =0;
	
	public static void hanoi(int n, int from, int via, int to) {
		if(n==1) {
			count++;		
			sb.append(from+" "+to+"\n");
		}
		else if(n<1) {
			return;
		}
		else {
			hanoi(n-1,from,to,via);
			hanoi(1,from,via,to);
			hanoi(n-1,via,from,to);
			
		}
	}
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		int n = in.nextInt();
		in.close();
		
		hanoi(n,1,2,3);
		sb.insert(0, count+"\n");
		System.out.println(sb.toString());
	}

}
반응형

'알고리즘 > 백준' 카테고리의 다른 글

[백준 1157] 단어공부 - 다시  (0) 2020.03.01
[백준 2675] 문자열 반복  (0) 2020.03.01
[백준 2477] 별찍기 -10  (0) 2020.02.26
[백준 10872]팩토리얼  (0) 2020.02.26
[백준 4949] 균형잡힌 세상  (0) 2020.02.05
Comments