728x90
반응형
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
반응형
'Algorithm Problems' 카테고리의 다른 글
[백준] [Python] 백트래킹 (0) | 2021.04.29 |
---|---|
[백준] [Python] 1074번 Z - 재귀 (0) | 2021.04.29 |
[백준] [Python] 1697번 숨바꼭질 - BFS (0) | 2021.04.29 |
[백준] [Python] 4179번 불! - BFS (0) | 2021.04.28 |
[백준] [Python] 7576번 토마토 - BFS (0) | 2021.04.28 |