forked from maxiee/MyCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hanoi.cpp
75 lines (62 loc) · 1.56 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
//
// Created by maxiee on 15-5-30.
//
#include <iostream>
using namespace std;
int const n = 5;
int stack[3][n] = {{1, 2, 3, 4, 5}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}};
int count = 0;
void move_rings(int n, int src, int dest, int other);
void print_stack();
int getTopNum(int numStack);
int getTopZero(int numStack);
void doMove(int src, int dest);
int main() {
print_stack();
move_rings(n, 0, 2, 1);
return 0;
}
void move_rings(int n, int src, int dest, int other) {
// cout << "move_rings(" << n << "," << src << "," << dest << "," << other << ")" << endl;
if (n == 1) {
doMove(src, dest);
} else {
move_rings(n - 1, src, other, dest);
doMove(src, dest);
move_rings(n - 1, other, dest, src);
}
}
void print_stack() {
cout << "***********第" << count << "轮**************" << endl;
int line;
for (line = 0; line < n; line++) {
cout << stack[0][line] << "\t" << stack[1][line] << "\t" << stack[2][line] << endl;
}
count ++;
}
int getTopNum(int numStack) {
int i;
for (i=0; i<n; i++) {
if (stack[numStack][i] != 0) {
return i;
}
}
return 3;
}
int getTopZero(int numStack) {
int i;
for (i=0; i<n-1; i++) {
if (stack[numStack][i+1] != 0 && stack[numStack][i] == 0) {
return i;
}
}
return n-1;
}
void doMove(int src, int dest){
int topSrc = getTopNum(src);
int topDest = getTopZero(dest);
int temp = stack[src][topSrc];
stack[dest][topDest] = temp;
stack[src][topSrc] = 0;
print_stack();
}