Please move all the disks from Tower A to Tower C (using Tower B as a buffer).
You may move one disk at a time such that no bigger disk would be on the top of a smaller one.
void Hanoi(int disk, String ^ source, String ^ spare, String ^ destination) //盤數,來源,暫存,目標
{ if (disk == 1) // 移動最頂的盤子到目標
{ counter++; // 移動步驟計數+1
listBox1->Items->Add("Step "+counter+": Move the top disk from tower "+Convert::ToString(source)+" to tower "+destination); //列印盤子的移動
}
else //遞迴呼叫Hanoi function執行
{ Hanoi(disk-1, source, destination, spare);
Hanoi(1, source, spare, destination);
Hanoi(disk-1, spare, source, destination);
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{ int disk = Convert::ToInt32(textBox1->Text); //決定盤子數量
counter = 0;
Hanoi(disk, "A", "B", "C"); //呼叫Honoi function
listBox1->Items->Add("----- "+counter+" steps in total for "+disk+" disks -----");
//顯示總步驟量
}