![8)](./images/smilies/icon_cool.gif)
[cpp]
#include <iostream>
#include <cmath>
using namespace std;
const int PROGRAM_END = 0;
const int MAX_DISKS = 100;
const int TOWER_COUNT = 3;
int movesToMake = 0;
int moves = 0;
bool quit = false;
void Recurise (int tower[], int numb, int begin, int finish, int hold);
void moveRing(int tower[], int to, int from);
int main()
{
int tower[TOWER_COUNT];
int diskCount = 0;
while(cin >> diskCount >> movesToMake)
{
if (diskCount == 0 && movesToMake == 0)
return 0;
if (movesToMake < 0 ||
diskCount < 0 ||
diskCount > MAX_DISKS ||
movesToMake > (pow(2,diskCount) -1))
{
continue;
}
moves = 0;
quit = false;
tower[0] = diskCount;
tower[1] = 0;
tower[2] = 0;
Recurise(tower,diskCount,0,1,2);
cout << tower[0] << " " << tower[1] << " " << tower[2] << endl;
}
return 0;
}
void Recurise (int tower[], int numb, int begin, int finish, int hold)
{
if (numb == 0)
return;
Recurise(tower, numb-1,begin, hold, finish);
moveRing(tower, begin, finish);
if (quit)
return;
Recurise(tower, numb-1,hold, finish, begin);
}
void moveRing(int tower[], int from, int to)
{
if (moves++ < movesToMake)
{
tower[to]++;
tower[from]--;
}
else
{
quit = true;
}
return;
}
[/cpp]