-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2addcc
commit fdf4feb
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#2023-04-27-Week3 | ||
#11729_하노이 탑.py | ||
|
||
|
||
''' | ||
하노이탑 원판 1개인 경우 -> top(1, 1, 2, 3) | ||
이동 과정 | ||
1. 1번 1->3 | ||
하노이탑 원판 2개인 경우 -> top(2, 1, 2, 3) | ||
이동 과정 | ||
1. 1번 1->2 | ||
2. 2번 1->3 | ||
3. 1번 2->3 | ||
하노이탑 원판 3개인 경우 -> top(3, 1, 2, 3) | ||
이동 과정 | ||
1. 1번 1->3 | ||
2. 2번 1->2 | ||
3. 1번 3->2 | ||
4. 3번 1->3 | ||
5. 1번 2->1 | ||
6. 2번 2->3 | ||
7. 1번 1->3 | ||
''' | ||
|
||
n = int(input()) | ||
|
||
def cnt(n) : | ||
if n==1 : | ||
k = 2 | ||
elif n<=0 : | ||
k = 1 | ||
else : | ||
k = 2*cnt(n-1) | ||
return k | ||
|
||
def top(n, a, b, c) : | ||
if n==1 : | ||
print(a,c) | ||
else : | ||
#else 코드는 스스로 작성하지 못함 | ||
top(n-1,a,c,b) | ||
print(a,c) | ||
top(n-1,b,a,c) | ||
|
||
''' | ||
함수 쓰지 않고 바로 계산해도 됨 | ||
sum = 2**n-1 | ||
print(sum) | ||
''' | ||
print(cnt(n)) | ||
top(n, 1, 2, 3) | ||
|