Learn Computer Science programming - C# and C++ Algorithms

 
Towers of Hanoi
The Tower of Hanoi was invented by the French mathematician Edouard Lucas in 1883. We are given a tower of eight disks initially stacked in increasing size on one of three pegs. The objective is to move the entire tower to one of the other peg while only moving only one disk at a time and not placing a larger one onto a smaller one.
Code
hanoi.zip
File Size: 3790 kb
File Type: zip
Download File

#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;

void showMove (int n, char src, char dst) 
{
    cout << "Move " << n << " from " << src << " to " << dst << endl;
}

void hanoi(int n, char src, char dst, char xtr) {
    // when there's just one left, put it where you want it
    if (n==1) {
showMove(n,src,dst);
        return;
    }
    // if there are more than 1, move all but last hoop to extra pole
    hanoi(n-1, src, xtr, dst);
showMove (n,src,dst);
    // after moving last hoop, put extra hoops back onto it
    hanoi(n-1, xtr, dst, src);
}

void main()
{

int x;

wcout <<  endl << "*** Hanoi ***" << endl;
wcout << "*************************************************" << endl;
    int n;
    cout << "Number of hoops: ";
    cin >> n;
hanoi (n, 'A', 'B', 'C');

cin >> x;
 }