ハノイの塔を4回回したいです。
# -*- coding: utf-8 -*- def hanoi(n, x, y, z): count = 0 while count < 4: count += 1 if count == 1: print("{}→{}".format(x, y)) else: hanoi(n-1, x, z, y) print("{}→{}".format(x, y)) hanoi(n-1, z, y, x) return count count = hanoi(3, "A", "C", "B")
とコードを書いて実行すると
RecursionError: maximum recursion depth exceeded while calling a Python object
と無限ループになってしまいました。
何が問題でどう直せば良いのでしょうか?
『4回回したい』とは具体的にはどのようなことをしたいのでしょうか。ハノイの塔を4回解きたいということですか?
円盤を4回動かしたいということです。この場合、7回動かして完成させるということですが、4回目で終了させたいということです。
回答2件
あなたの回答
tips
プレビュー