cses

solution to cses exercise problems
git clone git://git.superpozycja.net/cses
Log | Files | Refs | README

commit 19ab8d476ef42ae0d41b3f811c0dee991c43714e
parent 61fa124544a4f722913183cae0bb1091e248ba8c
Author: superpozycja <anna@superpozycja.net>
Date:   Thu, 24 Oct 2024 15:31:21 +0200

solve tower of hanoi (this one is clever)

Diffstat:
Aintro/tower_of_hanoi.cpp | 40++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+), 0 deletions(-)

diff --git a/intro/tower_of_hanoi.cpp b/intro/tower_of_hanoi.cpp @@ -0,0 +1,40 @@ +#include <bits/stdc++.h> + +using namespace std; + +using ui = unsigned int; +using l = long; +using ul = unsigned long; +using ll = long long; +using ull = unsigned long long; + +using vi = vector<int>; +using vui = vector<ui>; +using vl = vector<l>; +using vul = vector<ul>; +using vll = vector<ll>; +using vull = vector<ull>; + +void move(int n, int from, int to) { + if (n == 1) { + cout << from << " " << to << "\n"; + return; + } + + move(n-1, from, 6-(from+to)); /* ;) */ + cout << from << " " << to << "\n"; + move(n-1, 6-(from+to), to); +} + +void solve() +{ + int n; + cin >> n; + cout << (1<<n) - 1 << "\n"; + move(n, 1, 3); +} + +int main() +{ + solve(); +}