-
Notifications
You must be signed in to change notification settings - Fork 643
/
3.4.cpp
37 lines (33 loc) · 922 Bytes
/
3.4.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
#include <iostream>
#include <stack>
using namespace std;
struct op{
int begin, end;
char src, bri, dst;
op(){
}
op(int pbegin, int pend, int psrc, int pbri, int pdst):begin(pbegin), end(pend), src(psrc), bri(pbri), dst(pdst){
}
};
void hanoi(int n, char src, char bri, char dst){
stack<op> st;
op tmp;
st.push(op(1, n, src, bri, dst));
while(!st.empty()){
tmp = st.top();
st.pop();
if(tmp.begin != tmp.end){
st.push(op(tmp.begin, tmp.end-1, tmp.bri, tmp.src, tmp.dst));
st.push(op(tmp.end, tmp.end, tmp.src, tmp.bri, tmp.dst));
st.push(op(tmp.begin, tmp.end-1, tmp.src, tmp.dst, tmp.bri));
}
else{
cout<<"Move disk "<<tmp.begin<<" from "<<tmp.src<<" to "<<tmp.dst<<endl;
}
}
}
int main(){
int n = 3;
hanoi(n, 'A', 'B', 'C');
return 0;
}