# I don't understand the Haan Tower algorithm.

• Colleague, please explain how the Haan Towers code works.

``````#include <iostream>
#include <Windows.h>
using namespace std;
void Towers(int number, int from, int to, int free)
{
SetConsoleOutputCP(1251);
if(number!=0)
{
Towers(number-1, from, free, to);
cout<<"\n  Снимаем "<<number<<"-й диск с "<<from<<"-го стержня и одеваем его на "<<to<<"-й стержень";
Towers(number-1,  free, to, from);
}
}
void main()
{
Towers(3, 1, 3, 2);
cout<<"\n  ";
}
``````
• "number" is the color of the discs.
• "from" is a bitch with which we move all the disks.
• "to" is a bitch to carry all the disks.
• "free" is the third rod.

And this is a result in a console window:

```Снимаем 1-й диск с 1-го стержня и одеваем его на 3-й стержень. Снимаем 2-й диск с 1-го стержня и одеваем его на 2-й стержень. Снимаем 1-й диск с 3-го стержня и одеваем его на 2-й стержень. Снимаем 3-й диск с 1-го стержня и одеваем его на 3-й стержень. Снимаем 1-й диск с 2-го стержня и одеваем его на 1-й стержень. Снимаем 2-й диск с 2-го стержня и одеваем его на 3-й стержень. Снимаем 1-й диск с 1-го стержня и одеваем его на 3-й стержень.```

What I've learned here is that at the beginning of the job, she's a very sensitive one. It's been like, "if" and "number" every time. unit, comes to number=1, and the first line on the consoles is to be removed. Then The recurring challenge of the function, with arguments differently and fog. ♪ ♪

• Try to operate with physical objects (colps, paper strips or something). The idea is simple: to move `n` One rod to another, you have to. `n-1` Move it to a free (first recurring challenge), move the n- ring to the target stere.`cout`later `n-1` with the free transfer to the target (second challenge). When a man is appointed, the ring `from, to, free` Changes can be added to the conclusion of their values when converting to illustration. That's a condition not to put more rings on a smaller one. UPD In principle, three good presses in the procedure. The truth is, I'm not clear anymore. For the first challenge (single of ring above the lower to the auxiliary), to move the lower ring (existing cout) and to return the lower rings to the target.

``````#include <iostream>
#include <stdio.h>
using namespace std;
void Towers(int number, int from, int to, int free)
{
if(number!=0) {
fprintf(stderr, "%2d%3d%3d%3d/1\n", number-1, from, free, to);
Towers(number-1, from, free, to);
fprintf(stderr, "%2d%3d%3d%3d\n", number, from, to, free);
cout&lt;&lt;"\n  Снимаем "&lt;&lt;number&lt;&lt;"-й диск с "&lt;&lt;from&lt;&lt;"-го стержня и одеваем его на "&lt;&lt;to&lt;&lt;"-й стержень";
fprintf(stderr, "%2d%3d%3d%3d/2\n", number-1, free, to, from);
Towers(number-1,  free, to, from);
}

}
int main() {
cerr << " n  f  r  t" << endl;
Towers(3, 1, 3, 2);
cout<<"\n  ";
return 0;
}
``````

Conclusion (/1 and /2 -- challenges to Tower):

`````` n  f  r  t
2  1  2  3/1
1  1  3  2/1
0  1  2  3/1
1  1  3  2
0  2  3  1/2
2  1  2  3
1  3  2  1/2
0  3  1  2/1
1  3  2  1
0  1  2  3/2
3  1  3  2
2  2  3  1/2
1  2  1  3/1
0  2  3  1/1
1  2  1  3
0  3  1  2/2
2  2  3  1
1  1  3  2/2
0  1  2  3/1
1  1  3  2
0  2  3  1/2
``````

For induction proof: one ring is moved. Transmission of two -- move the top to standby, move the lower to target, move the top from reserve to target. Transporting several -- moving the top to the support pin, moving the ring number itself, then moving from the support to the target. Since the rings are smaller in diameter, their shift in the regulation is not disturbing.

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2