반응형
count = []
def hanoi(n,from0,to,via):
if n==1:
count.append([from0,to])
return
hanoi(n-1,from0,via,to)
count.append([from0,to])
hanoi(n-1,via,to,from0)
n = int(input())
hanoi(n,1,3,2)
print(len(count))
for item in count:
print(item[0],item[1])
하노이 탑의 원리는 n개를 옮길 때 n-1개를 1->2으로 3을 통해서 옮기고 맨 아래에 있는 원판 한개를 1->3 옮기고
다시 n-1개의 원판을 2->3 으로 1을 통해서 옮김.
반응형
'Algorithm > Baekjoon' 카테고리의 다른 글
2231 (0) | 2021.05.09 |
---|---|
2798 (0) | 2021.05.09 |
2581 (0) | 2021.05.08 |
1978 (0) | 2021.05.08 |
2839 (0) | 2021.05.08 |