#include "stdafx.h" #include #include #include using namespace std; const int n = 9; // pocet disku struct Vez { int cnt; // vyska veze, pocet disku ve vezi int pole [n]; // pole [0] ... velikost spodniho disku // pole [cnt-1] ... velikost vrcholoveho disku }; Vez A, B, C; void empty (Vez& X) { X.cnt = 0; for (int i = 0; i < n; i++) X.pole[i] = 0; } void init (Vez& X) { X.cnt = n; for (int i = 0; i < n; i++) X.pole[i] = n - i; } void print (Vez &X) { for (int i = 0; i < X.cnt; i++) cout << X.pole[i] << " "; for (int j = X.cnt; j < n; j++) cout <<" "; } void print () { print (A); cout << " : "; print (B); cout << " : "; print (C); cout << endl; } void step (Vez &X, Vez &Z) { // Z.pole[Z.cnt++] = X.pole[--X.cnt]; assert (X.cnt != 0); int d = X.pole[X.cnt - 1]; X.cnt--; assert (Z.cnt < n); if (Z.cnt > 0) assert (Z.pole[Z.cnt - 1] > d); Z.pole[Z.cnt] = d; Z.cnt++; print (); } void transfer (Vez &X, Vez &Y, Vez &Z, int k) { if (k>1) transfer (X, Z, Y, k - 1); step (X, Z); if (k>1) transfer (Y, X, Z, k - 1); } int main() { init (A); empty (B); empty (C); transfer (A, B, C, n); cout << "Konec programu" << endl; system ("pause"); return 0; }