2020-05-29 03:50:35 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Solve the [Tower of
|
|
|
|
* Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi) problem.
|
|
|
|
*/
|
2019-08-21 10:10:08 +08:00
|
|
|
#include <iostream>
|
2017-12-24 01:30:49 +08:00
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
/**
|
|
|
|
* Define the state of tower
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
struct tower
|
|
|
|
{
|
2020-05-29 03:50:35 +08:00
|
|
|
//! Values in the tower
|
|
|
|
int values[10];
|
|
|
|
//! top tower ID
|
|
|
|
int top;
|
2020-05-29 04:05:43 +08:00
|
|
|
};
|
2017-12-24 01:30:49 +08:00
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
/** Display the towers */
|
2020-05-29 04:05:43 +08:00
|
|
|
void show(const struct tower *const F, const struct tower *const T,
|
2020-05-30 07:26:30 +08:00
|
|
|
const struct tower *const U)
|
|
|
|
{
|
2020-05-29 03:50:35 +08:00
|
|
|
std::cout << "\n\n\tF : ";
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = 0; i < F->top; i++)
|
|
|
|
{
|
2020-05-29 04:05:43 +08:00
|
|
|
std::cout << F->values[i] << "\t";
|
2020-05-29 03:50:35 +08:00
|
|
|
}
|
|
|
|
std::cout << "\n\tU : ";
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = 0; i < U->top; i++)
|
|
|
|
{
|
2020-05-29 04:05:43 +08:00
|
|
|
std::cout << U->values[i] << "\t";
|
2020-05-29 03:50:35 +08:00
|
|
|
}
|
|
|
|
std::cout << "\n\tT : ";
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = 0; i < T->top; i++)
|
|
|
|
{
|
2020-05-29 04:05:43 +08:00
|
|
|
std::cout << T->values[i] << "\t";
|
2020-05-29 03:50:35 +08:00
|
|
|
}
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
/** Move one disc from one tower to another
|
|
|
|
* \param [in,out] From tower to move disk *from*
|
|
|
|
* \param [in,out] To tower to move disk *to*
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
void mov(tower *From, tower *To)
|
|
|
|
{
|
2020-05-29 04:05:43 +08:00
|
|
|
--From->top;
|
|
|
|
To->values[To->top] = From->values[From->top];
|
|
|
|
++To->top;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
/**
|
|
|
|
* Recursive algorithm to solve the puzzle
|
|
|
|
* \param [in] n starting number of disks
|
|
|
|
* \param [in,out] From tower to move disks from
|
|
|
|
* \param [in,out] Using temporary tower for the puzzle
|
|
|
|
* \param [in,out] To tower to move disk to
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
void TH(int n, tower *From, tower *Using, tower *To)
|
|
|
|
{
|
|
|
|
if (n == 1)
|
|
|
|
{
|
2020-05-29 03:50:35 +08:00
|
|
|
mov(From, To);
|
2020-05-29 04:05:43 +08:00
|
|
|
show(From, To, Using);
|
2020-05-30 07:26:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-29 03:50:35 +08:00
|
|
|
TH(n - 1, From, To, Using);
|
|
|
|
mov(From, To);
|
2020-05-29 04:05:43 +08:00
|
|
|
show(From, To, Using);
|
2020-05-29 03:50:35 +08:00
|
|
|
TH(n - 1, Using, From, To);
|
|
|
|
}
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
/** Main function */
|
2020-05-30 07:26:30 +08:00
|
|
|
int main()
|
|
|
|
{
|
2020-05-29 04:05:43 +08:00
|
|
|
struct tower F, U, T;
|
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
F.top = 0;
|
|
|
|
U.top = 0;
|
|
|
|
T.top = 0;
|
2017-12-24 01:30:49 +08:00
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
int no;
|
2017-12-24 01:30:49 +08:00
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
std::cout << "\nEnter number of discs : ";
|
|
|
|
std::cin >> no;
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = no; i > 0; i--)
|
|
|
|
{
|
2020-05-29 03:50:35 +08:00
|
|
|
F.values[F.top++] = i;
|
|
|
|
}
|
2017-12-24 01:30:49 +08:00
|
|
|
|
2020-05-29 04:05:43 +08:00
|
|
|
show(&F, &T, &U);
|
|
|
|
TH(no, &F, &U, &T);
|
2017-12-24 01:30:49 +08:00
|
|
|
|
2020-05-29 03:50:35 +08:00
|
|
|
return 0;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|