Advertisement
Guest User

Hanoi towers

a guest
Oct 30th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. struct node {
  8.     int tiles;
  9.     int from;
  10.     int to;
  11.     int help;
  12.  
  13.     node(int tiles, int from, int to, int help) {
  14.         this->tiles = tiles;
  15.         this->from = from;
  16.         this->to = to;
  17.         this->help = help;
  18.     }
  19. };
  20.  
  21. void hanoi_towers(int tiles, int from, int to, int help) {
  22.     stack<node> s;
  23.     s.push(node(tiles, from, to, help));
  24.     while (!s.empty())
  25.     {
  26.         node top = s.top();
  27.         s.pop();
  28.         if (top.tiles == 1) {
  29.             cout << "move(" << top.from << ", " << top.to << ")\n";
  30.         }
  31.         else {
  32.             s.push(node(top.tiles - 1, top.help, top.to, top.from));
  33.             s.push(node(1, top.from, top.to, top.help));
  34.             s.push(node(top.tiles - 1, top.from, top.help, top.to));
  35.         }
  36.     }
  37. }
  38.  
  39. int _tmain(int argc, _TCHAR* argv[])
  40. {
  41.     hanoi_towers(4, 1, 3, 2);
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement