圖示參考 https://blog.csdn.net/renlianggee/article/details/89945824
1 要把 n 個盤子從 A 移動到 C
2 那么必須把 n-1 個盤子從 A 移動到 B,這樣 最大的盤子 n 才能從 A 移動到 C
3 此時再把 n-1 個盤子 從 B 移動到 C。
本來我們的跳板是B, 步驟2 的跳板變成了 C。步驟3 的跳板 變成了A。
代碼如下:
void moveDisk(int disk, string src, string tar)
{
Console::out("盤子", disk, "? 從", src, "? 移動到", tar);
}
void hanno(int n, string src, string help, string tar)
{
? if (n == 1)
? {
??? moveDisk(1, src, tar);
? }else
? {
??? hanno(n - 1, src, tar, help);
??? moveDisk(n, src, tar);
??? hanno(n - 1, help,src, tar);
? }
}