From 8fcdba9741a0bfdf243691234a8662fbaa82fa22 Mon Sep 17 00:00:00 2001 From: nash4826 <84267693+nash4826@users.noreply.github.com> Date: Wed, 26 Jan 2022 22:06:18 +0900 Subject: [PATCH] chore(C): add tower of Hanoi using recursion (#681) --- algorithms/C/README.md | 6 +- algorithms/C/recursion/tower-of-hanoi.c | 101 ++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 algorithms/C/recursion/tower-of-hanoi.c diff --git a/algorithms/C/README.md b/algorithms/C/README.md index adb67ee7..ecae21ba 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -39,6 +39,10 @@ - [Double Ended Queue using array](queues/double-ended-queue-using-array.c) - [Circular Queue using array](queues/circular-queue-using-array.c) +## Recursion + +- [Tower of Hanoi](recursion/tower-of-hanoi.c) + ## Sorting - [Bubble Sort](sorting/bubble-sort.c) @@ -74,4 +78,4 @@ ## Stacks - [Stack using arrays](stacks/stack-using-arrays.c) -- [Stack using Linked List](stacks/stack-using-linked-list.c) \ No newline at end of file +- [Stack using Linked List](stacks/stack-using-linked-list.c) diff --git a/algorithms/C/recursion/tower-of-hanoi.c b/algorithms/C/recursion/tower-of-hanoi.c new file mode 100644 index 00000000..5e4e806b --- /dev/null +++ b/algorithms/C/recursion/tower-of-hanoi.c @@ -0,0 +1,101 @@ +#include +/* + *Tower of Hanoi* + + *Rule: + + 1. Only one disk can be moved at a time. + 2. Each move consists of taking the upper disk from one of the stacks + and placing it on top of another stack i.e. + a disk can only be moved if it is the uppermost disk on a stack. + 3. No disk may be placed on top of a smaller disk. + + * Time Complexity : O(2^n) + * Space Complexity: O(n) +*/ + + +void towerOfHanoi(int disk, char from, char by, char to) +{ + if (disk == 1) // If the number of disks to move is one + { + printf("Move disk 1 from %c to %c \n", from, to); + } + else + { + towerOfHanoi(disk - 1, from, to, by); + printf("Move disk %d from %c to %c \n", disk, from, to); + towerOfHanoi(disk - 1, by, from, to); + } + +} + +int main() +{ + int disk; + + printf("Enter the number of disks you want : "); + scanf("%d", &disk); + + // Move N disks of Tower A to Tower C via Tower B + towerOfHanoi(disk, 'A', 'B', 'C'); + + return 0; +} + +/* + Sample + + input | output + | + disk : 3 | Move disk 1 from A to C + | Move disk 2 from A to B + | Move disk 1 from C to B + | Move disk 3 from A to C + | Move disk 1 from B to A + | Move disk 2 from B to C + | Move disk 1 from A to C + + +--------------------------------------------------------------------------- + + + **Image illustration for 3 disks** + + + standby step 1 + + A B C A B C + + | | | | | | + | | | | | | + ###(1) | | | | | + #####(2) | | ##### | | +_#######(3)|______|__ _#######______|_______###_ + + + + step 2 step 3 step 4 + + A B C A B C A B C + + | | | | | | | | | + | | | | | | | | | + | | | | | | | | | + | | | | ### | | ### | +_#######_#####___###__ #######__#####____|___ __|_____#####__#######__ + + + + step 5 step 6 step 7 + + A B C A B C A B C + + | | | | | | | | | + | | | | | | | | | + | | | | | | | | ### + | | | | | ##### | | ##### +___###___#####_#######_ ___###_____|___#######_ __|________|_____#######__ + + +*/