Algorithm Problems
[백준] [Python] 11729번 하노이탑 이동순서 - 재귀 - [대표예제]
WakaraNai
2021. 4. 29. 20:10
728x90
반응형
11729번: 하노이 탑 이동 순서
세 개의 장대가 있고 첫 번째 장대에는 반경이 서로 다른 n개의 원판이 쌓여 있다. 각 원판은 반경이 큰 순서대로 쌓여있다. 이제 수도승들이 다음 규칙에 따라 첫 번째 장대에서 세 번째 장대로
www.acmicpc.net
Python
중간경로 표시
import sys
n = int(sys.stdin.readline().rstrip())
process = []
def hanoi(start, middle, end, n):
global process
if n==1:
process.append(str(start)+' '+str(end))
return
hanoi(start, end, middle, n-1)
process.append(str(start)+' '+str(end))
hanoi(middle, start, end, n-1)
hanoi(1,2,3,n)
print(len(process))
for path in process:
print(path)
중간경로 표시X
import sys
n = int(sys.stdin.readline().rstrip())
process = []
#middle = 6-(start+end)
def hanoi(start, end, n):
global process
if n==1:
process.append(str(start)+' '+str(end))
return
hanoi(start, 6-(start+end), n-1)
process.append(str(start)+' '+str(end))
hanoi(6-(start+end), end, n-1)
hanoi(1,3,n)
print(len(process))
for path in process:
print(path)
728x90
반응형