Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hanoi Tower.cpp
- #include "stdafx.h"
- #include <stdio.h>
- #include <iostream>
- #include <time.h>
- using namespace std;
- int iteration = 1;
- int top[3] = {0,0,0}; //указатель, (чтобы знать какому эл мы обращаемся)
- int const n = 3; //количество дисков
- int const first_peg = 1; //нумерация колышек
- int const end_peg = 3;
- int const mid_peg = 2;
- int stack[3][n];
- void add(int qty, int word);
- void del(int qty);
- void Hanoi_Towers(int numb, int from, int to, int buf_peg);
- int main()
- {
- setlocale(LC_ALL,"rus");
- long int t = clock();
- for (int i = n; i > 0; i--) {
- add(0, i);
- stack[1][i] = 0;
- stack[2][i] = 0;
- }
- cout << "My_tower: " << endl;
- for (int i = 0; i < n; i++) {
- cout << stack[0][i] << " " << stack[1][i] << " " << stack[2][i] << "\n";
- }
- cout << "\n";
- Hanoi_Towers(n, first_peg, end_peg, mid_peg);
- cout << "The time of execute the program " << clock() - t << " ms. " <<endl;
- system("pause");
- return 0;
- }
- //добавляем элементы в стек
- void add(int qty, int word) {
- if (top[qty] >= n) {
- cout << "Full stack!!!!!!!!!" << endl;
- }
- else {
- for (int i = top[qty] - 1; i >= 0; i--)
- stack[qty][i + 1] = stack[qty][i];
- stack[qty][0] = word;
- top[qty]++;
- }
- }
- //удаление элементов со стека
- void del(int qty) {
- if (top[qty] <= 0) {
- cout << "Empty stack!!!!!!!" << endl;
- }
- else {
- for (int i = 0; i < (top[qty] - 1); i++)
- stack[qty][i] = stack[qty][i + 1];
- top[qty]--;
- stack[qty][top[qty]] = 0;
- }
- }
- void Hanoi_Towers(int numb, int from, int to, int buf_peg)
- {
- if (numb != 0)
- {
- Hanoi_Towers(numb - 1, from, buf_peg, to);
- cout << "Iteration " << iteration << ": " << from << " --> " << to << endl;
- iteration++;
- add(to - 1, stack[from - 1][0]);
- del(from - 1);
- for (int i = 0; i < n; i++) cout << stack[0][i] << " " << stack[1][i] << " " << stack[2][i] << "\n";
- cout << "\n";
- Hanoi_Towers(numb - 1, buf_peg, to, from);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement