Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <stack>
- using namespace std;
- struct node {
- int tiles;
- int from;
- int to;
- int help;
- node(int tiles, int from, int to, int help) {
- this->tiles = tiles;
- this->from = from;
- this->to = to;
- this->help = help;
- }
- };
- void hanoi_towers(int tiles, int from, int to, int help) {
- stack<node> s;
- s.push(node(tiles, from, to, help));
- while (!s.empty())
- {
- node top = s.top();
- s.pop();
- if (top.tiles == 1) {
- cout << "move(" << top.from << ", " << top.to << ")\n";
- }
- else {
- s.push(node(top.tiles - 1, top.help, top.to, top.from));
- s.push(node(1, top.from, top.to, top.help));
- s.push(node(top.tiles - 1, top.from, top.help, top.to));
- }
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- hanoi_towers(4, 1, 3, 2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement