https://shoark7.github.io/programming/algorithm/tower-of-hanoi
하노이의 탑#
재귀, 브루트포스, 완전탐색, 백트래킹 유명한 문제이기 때문에 잘 정리를 해놓는 것이 좋을 것으로 보인다. 이거 다음에 백준에서 재귀연습문제 덩어리 풀어보기
https://www.acmicpc.net/workbook/view/2052
# input setting
problem_num = "하노이의 탑"
import os, sys
path = os.getcwd() + f"\\txt\\{problem_num}" + ".txt"
sys.stdin = open(path, "r")
n = int(sys.stdin.readline())
result = []
def move(n, start, to):
return [start, to]
def hanoi(n, start, to, via):
if n == 1:
result.append(move(1, start, to))
else:
hanoi(n - 1, start, via, to)
result.append(move(n, start, to))
hanoi(n - 1, via, to, start)
return result
def solution(n):
start, via, to = 1, 2, 3
answer = hanoi(n, start, via, to)
return answer
solution(4)
[[1, 2],
[1, 3],
[2, 3],
[1, 2],
[3, 1],
[3, 2],
[1, 2],
[1, 3],
[1, 2],
[3, 2],
[1, 3],
[2, 1],
[2, 3],
[1, 3],
[1, 2],
[3, 2],
[3, 1],
[2, 1],
[3, 2],
[1, 3],
[1, 2],
[3, 2]]