:: [intv] Towers of Hanoi (recursive solution) ::
HOME


[Date Prev][Date Next][Date Index]

[intv] Towers of Hanoi (recursive solution)



//---------------main.cpp----------- //Problem: Tower of Hanoi. //Save as: main.cpp, compile as: g++ -o main main.cpp

#include <iostream>
using namespace std;
void TOH(int n, char *l, char *m, char *r){
  if (n == 2) {
    cout << "Disc 1: " << l << " -> " << m << endl;
    cout << "Disc 2: " << l << " -> " << r << endl;
    cout << "Disc 1: " << m << " -> " << r << endl;
    return;
  }

  TOH(n-1, l, r, m);
  cout << "Disc " << n << ": " << l << " -> " << r << endl;
  TOH(n-1, m, l, r);
}

int main(){
  TOH(5, "left", "middle", "right");

}

//---------------main.cpp-----------


Comments and corrections are appreciated and can be sent to papers@mia.ece.uic.edu. Click here for ©opyright information.