Files
hello-algo/ru/codes/python/chapter_divide_and_conquer/hanota.py
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

54 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
File: hanota.py
Created Time: 2023-07-16
Author: krahets (krahets@163.com)
"""
def move(src: list[int], tar: list[int]):
"""Переместить один диск"""
# Снять диск с вершины src
pan = src.pop()
# Положить диск на вершину tar
tar.append(pan)
def dfs(i: int, src: list[int], buf: list[int], tar: list[int]):
"""Решить задачу Ханойской башни f(i)"""
# Если в src остался только один диск, сразу переместить его в tar
if i == 1:
move(src, tar)
return
# Подзадача f(i-1): переместить верхние i-1 дисков из src в buf с помощью tar
dfs(i - 1, src, tar, buf)
# Подзадача f(1): переместить оставшийся один диск из src в tar
move(src, tar)
# Подзадача f(i-1): переместить верхние i-1 дисков из buf в tar с помощью src
dfs(i - 1, buf, src, tar)
def solve_hanota(A: list[int], B: list[int], C: list[int]):
"""Решить задачу Ханойской башни"""
n = len(A)
# Переместить верхние n дисков из A в C с помощью B
dfs(n, A, B, C)
"""Driver Code"""
if __name__ == "__main__":
# Хвост списка соответствует вершине столбца
A = [5, 4, 3, 2, 1]
B = []
C = []
print("Исходное состояние:")
print(f"A = {A}")
print(f"B = {B}")
print(f"C = {C}")
solve_hanota(A, B, C)
print("После завершения перемещения дисков:")
print(f"A = {A}")
print(f"B = {B}")
print(f"C = {C}")