0
#include <iostream>

using namespace std;


//Write a recursive algorithm to solve the towers of Hanoi problem.
//move disc 1 A to c ACB
//move disc 2 A to b ABC
//move disc 1 C to b CBA
//move disc 3 A to c ACB
//move disc 1 B to a BAC
//move disc 2 B to c BCA
//move disc 1 A to c ACB


int moves(0);
void Hanoi(int m, char a, char b, char c);

void Hanoi(int m, char a, char b, char c){
  moves++;
  if(m == 1){
    cout << "Move disc " << m << " from " << a << " to " << c << endl;
  }else{  
    Hanoi(m-1, a,c,b);
    cout << "Move disc " << m << " from " << a << " to " << c << endl;
    Hanoi(m-1,b,a,c);
  }
}

int main(){

  int discs;
  cout << "Enter the number of discs: " << endl;
  cin >> discs;
  Hanoi(discs, 'A', 'B', 'C');
  cout << "It took " << moves << " moves. " << endl;

  system("pause");
}

i don't understand how the recursion work
plz some one trace it for me or explain in details

2
Contributors
2
Replies
35
Views
2 Years
Discussion Span
Last Post by Ahmed_62
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.