본문 바로가기

Algorithm/BOJ 기초

[백준] 하노이 탑 이동 순서 11729 Python (재귀)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cnt = 0
result = []
def hanoi(n, fromm, by, to):
    global cnt
    cnt += 1
    if n == 1:
        result.append(str(fromm)+" "+str(to))
    else:
        hanoi(n-1, fromm, to, by)
        result.append(str(fromm)+" "+str(to))
        hanoi(n-1, by, fromm, to)
 
= int(input())
hanoi(n,1,2,3)
 
print(cnt)
for i in result:
    print(i)