-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTOH.cpp
More file actions
46 lines (43 loc) · 953 Bytes
/
Copy pathTOH.cpp
File metadata and controls
46 lines (43 loc) · 953 Bytes
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
//operation choices: DOMOVE will move a disk
//DOTOH corresponds to a recursive call
enum TOHop {DOMOVE, DOTOH};
class TOHobj
{
public:
TOHop op;
int num;
Pole start, goal, tmp;
//DOTOH operation constructor
TOHobj(int n,Pole s, Pole g, Pole t)
{
op = DOTOH; num = n;
start = s; goal = g; tmp = t;
}
};
//DOMOVE operation constructor
TOHobj(Pole s, Pole g)
{
op = DOMOVE; start = s; goal = g;
};
void TOH(int n, Pole start, Pole goal, Pole tmp, Stack<TOHobj*>& S)
{
S.push(new TOHobj(n, start, goal, tmp));
TOHobj* t;
while (S.length()>0)
{
t=S.pop();
if (t->op == DOMOVE)
move(t->start, t->goal);
else if (t->num > 0)
{
int num = t->num;
Pole tmp = t->tmp;
Pole goal = t->goal;
Pole start = t->start;
S.push(new TOHobj(num-1, tmp, goal, start));
S.push(new TOHobj(start, goal));
S.push(new TOHobj(num-1, start, tmp,goal));
}
delete t;
}
}