0
私の友人が彼のハノイの塔のために書いたコードを読んでいます。しかし、彼の初期設定と終了設定がわからないので、自分のコードが何をするのか分かりにくいです。ハノイ塔の初期設定?
def T(init, final):
if len(init) == 0:
return
if init[0] == final[0]:
T(init[1:], final[1:])
else:
fro = init[0]
to = final[0]
spare = other(init[0], final[0])
ic = spare * (len(init) - 1)
T(init[1:], ic)
print("move from %s to %s " % (fro, to))
T(ic, final[1:])
def other(char1, char2):
towers = "ABC"
towers = towers.replace(char1, "")
towers = towers.replace(char2, "")
return towers
init = "ABCBA"
final = "BCBAC"
T(init, final)
ここで、彼はinit = "ABCBA"とfinal = "BCBAC"を持っています。コードは正常に動作しますが、なぜ彼はこれをやっているのですか?
何か助けていただければ幸いです。