-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path227.h
More file actions
75 lines (68 loc) · 1.67 KB
/
227.h
File metadata and controls
75 lines (68 loc) · 1.67 KB
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
class Tower {
private:
stack<int> disks;
public:
/*
* @param i: An integer from 0 to 2
*/
Tower(int i) {
// create three towers
}
/*
* @param d: An integer
* @return: nothing
*/
void add(int d) {
// Add a disk into this tower
if (!disks.empty() && disks.top() <= d) {
printf("Error placing disk %d", d);
} else {
disks.push(d);
}
}
/*
* @param t: a tower
* @return: nothing
*/
void moveTopTo(Tower &t) {
// Move the top disk of this tower to the top of t.
if(disks.empty()) return;
int d = disks.top();
disks.pop();
t.add(d);
}
/*
* @param n: An integer
* @param destination: a tower
* @param buffer: a tower
* @return: nothing
*/
void moveDisks(int n, Tower &destination, Tower &buffer) {
// Move n Disks from this tower to destination by buffer tower
cout << n - 1 << endl;
if(n <= 0)
return;
if(n == 1){
moveTopTo(destination);
return;
}
moveDisks(n-1, buffer, destination);
moveDisks(1, destination, buffer);
buffer.moveDisks(n-1, destination, *this);
}
/*
* @return: Disks
*/
stack<int> getDisks() {
// write your code here
return disks;
}
};
/**
* Your Tower object will be instantiated and called as such:
* vector<Tower> towers;
* for (int i = 0; i < 3; i++) towers.push_back(Tower(i));
* for (int i = n - 1; i >= 0; i--) towers[0].add(i);
* towers[0].moveDisks(n, towers[2], towers[1]);
* print towers[0], towers[1], towers[2]
*/