1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
using namespace std;
int towerHanoi(int disks, char a, char b, char c);
int main()
{
int disks, count;
char a, b , c;
cout <<"Welcome to The Tower of Hanoi!!! " << endl;
cout <<" " << endl;
cout <<"How many disks? " << endl;
cin >> disks;
towerHanoi(disks, 'A', 'B', 'C');
cout <<" Number of turns: " << count << endl;
system("pause");
return 0;
}
int towerHanoi(int disks, char a, char b, char c)
{
int count = 0;
if(disks == 0)
{
cout <<"Move disk from :" <<a<< " to " <<b << endl;
count = count +1;
}
else
{
towerHanoi(disks - 1, a, c, b);
count = count +1;
cout <<"Move disk from :" <<a<< " to " <<b << endl;
towerHanoi(disks - 1, c, b, a);
count = count +1;
}
return count;
}
|