Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
1] Only one disk can be
moved at a time.
2] Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3] No disk may be placed on top of a smaller disk.
Note: Transferring the top n-1 disks from source rod to Auxiliary rod can again be thought of as a fresh problem and can be solved in the same manner.
Python3
Bàn luận
Tower of Hà Nội là một câu đố toán học nơi chúng tôi có ba thanh và đĩa N. Mục tiêu của câu đố là di chuyển toàn bộ ngăn xếp sang một thanh khác, tuân theo các quy tắc đơn giản sau: & nbsp; 1] Chỉ có thể di chuyển một đĩa. các ngăn xếp và đặt nó lên trên một ngăn xếp khác, tức là một đĩa chỉ có thể được di chuyển nếu đó là đĩa trên cùng trên ngăn xếp. & nbsp; 3] không có đĩa nào được đặt trên đầu của một đĩa nhỏ hơn. 1 đĩa từ thanh nguồn đến thanh phụ trợ một lần nữa có thể được coi là một vấn đề mới và có thể được giải quyết theo cách tương tự. & Nbsp;
def
TowerOfHanoi[n , source, destination, auxiliary]:
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles1
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles9
def
1def
21
def
4
Các
def
1def
21
TowerOfHanoi[n , source, destination, auxiliary]:
8
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles1
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles2
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles3
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles4
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles5
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles6
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles7
2
3
4
5
4
7
8
def TowerOfHanoi[n , s_pole, d_pole, i_pole]:
if n == 1:
print["Move disc 1 from pole",s_pole,"to pole",d_pole]
return
TowerOfHanoi[n-1, s_pole, i_pole, d_pole]
print["Move disc",n,"from pole",s_pole,"to pole",d_pole]
TowerOfHanoi[n-1, i_pole, d_pole, s_pole]
n = 3
TowerOfHanoi[n, 'A', 'C', 'B']
# A, C, B are the name of poles
2 def TowerOfHanoi[n , s_pole, d_pole, i_pole]:
if n == 1:
print["Move disc 1 from pole",s_pole,"to pole",d_pole]
return
TowerOfHanoi[n-1, s_pole, i_pole, d_pole]
print["Move disc",n,"from pole",s_pole,"to pole",d_pole]
TowerOfHanoi[n-1, i_pole, d_pole, s_pole]
n = 3
TowerOfHanoi[n, 'A', 'C', 'B']
# A, C, B are the name of poles
3def
8def
9TowerOfHanoi[n , source, destination, auxiliary]:
0____15def TowerOfHanoi[n , s_pole, d_pole, i_pole]:
if n == 1:
print["Move disc 1 from pole",s_pole,"to pole",d_pole]
return
TowerOfHanoi[n-1, s_pole, i_pole, d_pole]
print["Move disc",n,"from pole",s_pole,"to pole",d_pole]
TowerOfHanoi[n-1, i_pole, d_pole, s_pole]
n = 3
TowerOfHanoi[n, 'A', 'C', 'B']
# A, C, B are the name of poles
6def TowerOfHanoi[n , s_pole, d_pole, i_pole]:
if n == 1:
print["Move disc 1 from pole",s_pole,"to pole",d_pole]
return
TowerOfHanoi[n-1, s_pole, i_pole, d_pole]
print["Move disc",n,"from pole",s_pole,"to pole",d_pole]
TowerOfHanoi[n-1, i_pole, d_pole, s_pole]
n = 3
TowerOfHanoi[n, 'A', 'C', 'B']
# A, C, B are the name of poles
7
Move disk 1 from source A to destination C Move disk 2 from source A to destination B Move disk 1 from source C to destination B Move disk 3 from source A to destination C Move disk 1 from source B to destination A Move disk 2 from source B to destination C Move disk 1 from source A to destination C Move disk 4 from source A to destination B Move disk 1 from source C to destination B Move disk 2 from source C to destination A Move disk 1 from source B to destination A Move disk 3 from source C to destination B Move disk 1 from source A to destination C Move disk 2 from source A to destination B Move disk 1 from source C to destination B
TowerOfHanoi[n , source, destination, auxiliary]:
9=
1O[2n]
Đầu raO[n]
Độ phức tạp về thời gian: O [2N]
Tháp Hà Nội là một vấn đề toán học [câu đố] bao gồm 3 cực và số lượng đĩa, mỗi đĩa có đường kính khác nhau. Mục tiêu hoặc mục tiêu của vấn đề này là chuyển tất cả các đĩa ’n từ cực nguồn sang cực đích theo cách mà chúng ta có được sự sắp xếp tương tự của các đĩa như trước đây. Nhưng mục tiêu này phải đạt được bằng cách tuân thủ các quy tắc. Các ràng buộc phải được thỏa mãn trong khi giải quyết vấn đề là - Hình ảnh sau đây cho thấy giải pháp riêng cho một tòa tháp Hà Nội với 3 cực [nguồn, trung gian, đích] và 3 đĩa. Mục tiêu là di chuyển tất cả 3 đĩa từ cực A sang cực C.Mục tiêu của Tháp Hà Nội
Quy tắc và ràng buộc
Đại diện trực quan của Tháp Hà Nội of the Tower of Hanoi problem
Như chúng ta có thể thấy từ giải pháp trên, số lượng di chuyển cần thiết cho 3 đĩa = 8. Vì vậy, một công thức tổng quát cho tổng số lượng di chuyển chúng ta cần là:
Tổng số di chuyển = n2 & nbsp; - 1
Trong đó ’n, là tổng số không. của đĩa.
Giải quyết vấn đề của Hà Hà hội ở Python
def TowerOfHanoi[n , s_pole, d_pole, i_pole]: if n == 1: print["Move disc 1 from pole",s_pole,"to pole",d_pole] return TowerOfHanoi[n-1, s_pole, i_pole, d_pole] print["Move disc",n,"from pole",s_pole,"to pole",d_pole] TowerOfHanoi[n-1, i_pole, d_pole, s_pole] n = 3 TowerOfHanoi[n, 'A', 'C', 'B'] # A, C, B are the name of poles
Trong mã trên, chúng tôi gọi chức năng của chúng tôi TowerofHanoi đã đệ quy cho 3 đĩa.
Here:
- s_pole: cực nguồn
- I_pole: Cực trung gian
- D_pole: Cực đích
Đầu ra của mã trên là:
Sự kết luận
Vì vậy, đây là cách chúng tôi giải quyết vấn đề của Tháp Hà Nội.
Mã này có thể được khái quát cho bất kỳ số lượng đĩa. Vì vậy, nếu bạn muốn giải pháp cho 4 đĩa, chỉ cần thay đổi giá trị của n từ 3 thành 4 là n = 4 và đầu ra sẽ được hiển thị cho 4 đĩa, v.v.