Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Introductory Problems/Tower of Hanoi/divyanshu1150.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
using namespace std;

void solve(int n, int current, int help, int dest){
//base case for 1 disk
if(n == 1){
cout << current << " " << dest << endl;
return;
}

//solve for upper n-1 disk from current to help stack
solve(n-1, current, dest, help);

//move the largest disk to the distination
cout << current << " " << dest << endl;

//solve for remaining n-1 disk from help to destination
solve(n-1, help, current, dest);
}

int main(){
int n;
cin >> n;
cout << pow(2, n) - 1 << endl;
int current = 1, help = 2, dest = 3;

//transfer n disks from current stack to destination stack using help stack
solve(n, current, help, dest);
return 0;
}