Skip to content

Commit fdf4feb

Browse files
committed
#14 : 11729_하노이 탑
1 parent f2addcc commit fdf4feb

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

이티준희/11729_하노이탑.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#2023-04-27-Week3
2+
#11729_하노이 탑.py
3+
4+
5+
'''
6+
하노이탑 원판 1개인 경우 -> top(1, 1, 2, 3)
7+
이동 과정
8+
1. 1번 1->3
9+
10+
하노이탑 원판 2개인 경우 -> top(2, 1, 2, 3)
11+
이동 과정
12+
1. 1번 1->2
13+
2. 2번 1->3
14+
3. 1번 2->3
15+
16+
하노이탑 원판 3개인 경우 -> top(3, 1, 2, 3)
17+
이동 과정
18+
1. 1번 1->3
19+
2. 2번 1->2
20+
3. 1번 3->2
21+
4. 3번 1->3
22+
5. 1번 2->1
23+
6. 2번 2->3
24+
7. 1번 1->3
25+
26+
'''
27+
28+
n = int(input())
29+
30+
def cnt(n) :
31+
if n==1 :
32+
k = 2
33+
elif n<=0 :
34+
k = 1
35+
else :
36+
k = 2*cnt(n-1)
37+
return k
38+
39+
def top(n, a, b, c) :
40+
if n==1 :
41+
print(a,c)
42+
else :
43+
#else 코드는 스스로 작성하지 못함
44+
top(n-1,a,c,b)
45+
print(a,c)
46+
top(n-1,b,a,c)
47+
48+
'''
49+
함수 쓰지 않고 바로 계산해도 됨
50+
sum = 2**n-1
51+
print(sum)
52+
'''
53+
print(cnt(n))
54+
top(n, 1, 2, 3)
55+

0 commit comments

Comments
 (0)