#include <iostream> #include <vector> using namespace std; void towers( vector <int> &disks, int start, int end, int temp) { if( disks == 1 ) // base case cout <<start <<"---> "<< end <<"\n"; else //recursion step { //move disks - 1 disk from start to temp towers(disks - 1, start, temp, end); //move last disk from start to end cout<<start<< "---> "<< end << "\n"; //move disks - 1 disk from temp to end towers(disks - 1, temp, end, start); } // end else } // end function towers int main() { int n; cout<<"Enter number of Discs: "; cin >> n; towers(n, 1, 3, 2); return 0; }
No comments:
Post a Comment