Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution to Tower-of-Hanoi #49

Merged
merged 1 commit into from Oct 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions CSES-SOLUTIONS/Introductory-Problems/Tower-of-Hanoi/sanam2405.cpp
@@ -0,0 +1,52 @@
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef long double ld;

vector<string> v;
void TOH(int n, int a, int b, int c)
{
if (n > 0) {
TOH(n - 1, a, c, b);
string s = to_string(a) + " " + to_string(c);
v.push_back(s);
TOH(n - 1, b, a, c);
}

}
void solve()
{

ll n;
cin >> n;
TOH(n, 1, 2, 3);
cout << v.size() << "\n";
for (ll i = 0; i < v.size(); i++)
cout << v[i] << "\n";
}



int main() {

#ifndef ONLINE_JUDGE
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
#endif

ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);

ll t;
t = 1;
// cin >> t;
while (t--)
{
solve();
}

return 0;
}