#include #include using namespace std; void PrintMove(string a, string b, string c); void DoTowers(string & src, string & dest, string & aux, int size); int main() { int count; string towerA="A:", towerB="B:", towerC="C:"; char c; cout << "Enter the number of disks" << endl; cin >> count; cout << endl; if (count > 26) { cout << "Sorry, too many disks" << endl; } else { for(c = char('a'+count-1) ; c >= 'a'; c--) { towerA += c; } PrintMove(towerA, towerB, towerC); DoTowers(towerA, towerC, towerB, count); } return 0; } void PrintRest(string a, string b) { if (a[0] == 'B') { cout << a << endl; cout << b << endl; } else { cout << b << endl; cout << a << endl; } return; } void PrintMove(string a, string b, string c) { cout << endl; if (a[0] == 'A') { cout << a << endl; PrintRest(b,c); } else if (b[0] == 'A') { cout << b << endl; PrintRest(a,c); } else { cout << c << endl; PrintRest(a,b); } return; } void MoveADisk(string & src, string & dest, string & aux) { // put the disk on dest dest += src.back(); // remove it from src src = src.substr(0,src.size()-1); PrintMove(src, dest, aux); return; } void DoTowers(string & src, string & dest, string & aux, int size){ if (size == 0) { cout << "Nothing to do " << endl; return; } if (size == 1) { MoveADisk(src, dest,aux); } else { // move smaller stack to aux DoTowers(src, aux, dest, size-1); MoveADisk(src, dest,aux); // move smaller stack to dest DoTowers(aux, dest, src, size-1); } return; }