-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHanoi.cpp
99 lines (83 loc) · 2.38 KB
/
Hanoi.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
bool Stack1[64] = {false};
bool Stack2[64] = {false};
bool Stack3[64] = {false};
bool verbose = false;
bool graph = false;
void Init(const int iSize)
{
int i =0;
for(i=0;i<iSize;++i)
{
Stack1[i] = true;
}
}
void PrintStacks()
{
system("clear");
int i =0;
cout << "1 2 3 " << endl;
for(i=63;i>=0;i--)
{
if(Stack1[i] || Stack2[i] || Stack3[i])
{
cout <<
(Stack1[i] ? "_ ":" " ) <<
(Stack2[i] ? " _ ":" " ) <<
(Stack3[i] ? " _ ":" " ) << endl;
}
}
//bottom
cout << "__________________________" << endl << endl;
}
void Move(bool* myStack1, bool* myStack2, bool* myStack3, int cnt, int from, int to)
{
//move the first cnt slides from stack1 to stack3
if(cnt > 0)
{
Move(myStack1,myStack3,myStack2,cnt-1,1,2);
if(verbose)
cout << "Moving Slide " << cnt << " from " << from<< " to " << to << endl;
if(graph)
PrintStacks();
myStack1[cnt-1] = false;
myStack3[cnt-1] = true;
Move(myStack2,myStack1,myStack3,cnt-1,2,3);
if(graph)
PrintStacks();
}
}
int main(int argc, char** argv)
{
if (argc > 1)
{
if(argc >2)
{
if(!strncmp(argv[2],"g",1))
graph = true;
if(!strncmp(argv[2],"v",1))
verbose = true;
}
if(argc >3)
{
if(!strncmp(argv[3],"g",1))
graph = true;
if(!strncmp(argv[3],"v",1))
verbose = true;
}
int Nb = atoi(argv[1]);
cout << "Starting with " << Nb << " Slides. graph =" << graph
<< " verbose=" << verbose << endl;
cout << "arguments: " << endl;
for (int i =0;i<argc;++i)
cout << i <<": " << "\"" << argv[i] << "\"" << endl;
Init(Nb);
PrintStacks();
Move(Stack1, Stack2,Stack3,Nb,1,3);
PrintStacks();
}
return 0;
}