Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Tower of hanoi
//g++ 7.4.0 #include <bits/stdc++.h> using namespace std; int m=0; void TOH(int n, int a, int b, int c) { if( n==1) { cout<<"\n Move disk: "<<n<<" from "<<a <<" to "<<b<<endl; cout<<" Move disk: "<<n<<" from "<<b <<" to "<<c<<endl; m+=2; } else { TOH(n-1,a,b,c); cout<<"\n Move largest disk: "<<n<<" from disk "<<a <<" to "<<b<<endl; m++; TOH(n-1,c,b,a); cout<<"\nMove largest disk: "<<n<<" from "<<b <<" to "<<c<<endl; m++; TOH(n-1,a,b,c); } } int main() { int n,a=1,b=2,c=3; cin>>n; TOH(n,a,b,c); cout<<"Total moves:"<<m; return 0; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Mirror a binary tree
Synchro#2
Arithmetic
Dejalo a la Suerte
numberOftweets
Einstein's Physics
3
VectorNormFn
¡Arriba, Papalotes, Arriba!
string-Orderly words
Please log in to post a comment.