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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>
using namespace std;
int NbElem = 3;
void Hanoi(int Matr[3][3], int NbElem, int Col1, int Col2, int Col3);
void Deplacement(int Matr[3][3], int Col1, int Col3);
void Afficher(int Matr[3][3]);
void main(void) {
int Col1 = 0;
int Col2 = 1;
int Col3 = 2;
int Matr[3][3] = {{1,0,0},{2,0,0},{3,0,0}};
cout << "Nombre d'anneaux ? : 3" << endl << endl << endl << endl << endl;
cout << left << setw(8) << "1" << left << setw(8) << "." << left << setw(8) << "." << endl;
cout << left << setw(8) << "2" << left << setw(8) << "." << left << setw(8) << "." << endl;
cout << left << setw(8) << "3" << left << setw(8) << "." << left << setw(8) << "." << endl;
cout << "____________________" << endl << endl << endl;
Hanoi(Matr, NbElem, Col1, Col2, Col3);
_getch();
}
void Hanoi(int Matr[3][3], int NbElem, int Col1, int Col2, int Col3) {
if(NbElem == 0) {
; // condition d'arrêt
}
else {
Hanoi(Matr, NbElem-1,Col1, Col2, Col3);
Deplacement(Matr, Col1, Col3);
Hanoi(Matr,NbElem-1, Col2, Col1, Col3);
}
}
void Deplacement(int Matr[3][3], int Col1, int Col3) {
int tempo = 0;
int i = 0;
while(Matr[i][Col1]==0) {
i++;
}
if(i<3) {
tempo = Matr[i][Col1];
Matr[i][Col1] = 0;
}
i = NbElem-1;
while(Matr[i][Col3] != 0) {
i--;
}
if(i>=0) {
Matr[i][Col3] = tempo;
}
Afficher(Matr);
_getch();
}
void Afficher(int Matr[3][3]) {
cout << left << setw(8) << Matr[0][0] << left << setw(8) << Matr[0][1] << left << setw(8) << Matr[0][2] << endl;
cout << left << setw(8) << Matr[1][0] << left << setw(8) << Matr[1][1] << left << setw(8) << Matr[1][2] << endl;
cout << left << setw(8) << Matr[2][0] << left << setw(8) << Matr[2][1] << left << setw(8) << Matr[2][2] << endl;
cout << "____________________" << endl << endl;
} |
Partager