From dcef6fef864949bcde09907c120180733e5d37bc Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 17 Feb 2025 11:43:13 -0700 Subject: 2165. tower of hanoi --- introductory_problems/tower_of_hanoi.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 introductory_problems/tower_of_hanoi.cpp (limited to 'introductory_problems/tower_of_hanoi.cpp') diff --git a/introductory_problems/tower_of_hanoi.cpp b/introductory_problems/tower_of_hanoi.cpp new file mode 100644 index 0000000..806fc51 --- /dev/null +++ b/introductory_problems/tower_of_hanoi.cpp @@ -0,0 +1,21 @@ +// 2165. Tower of Hanoi +#include +using namespace std; + +void move(int from, int to, int depth) { + if (depth == 1) { + printf("%d %d\n", from, to); + return; + } + int other = 6 - from - to; + move(from, other, depth - 1); + printf("%d %d\n", from, to); + move(other, to, depth - 1); +} + +int main() { + static int n; + scanf("%d", &n); + printf("%d\n", (1 << n) - 1); + move(1, 3, n); +} -- cgit v1.2.3