#Towers of Hanoi in C using Recursion
Explore tagged Tumblr posts
Text
Solved CS211 Non-recursive solution for Towers of Hanoi
Using the algorithm discussed in class, write an iterative program to solve the Towers of Hanoi problem. The problem: You are given three towers a, b, and c. We start with n rings on tower a and we need to transfer them to tower b subject to the following restrictions: 1. We can only move one ring at a time, and 2. We may never put a larger numbered ring on top of a smaller numbered one. There…
0 notes
Text
CS211 Non-recursive solution for Towers of Hanoi
Using the algorithm discussed in class, write an iterative program to solve the Towers of Hanoi problem. The problem: You are given three towers a, b, and c. We start with n rings on tower a and we need to transfer them to tower b subject to the following restrictions: 1. We can only move one ring at a time, and 2. We may never put a larger numbered ring on top of a smaller numbered one. There…
0 notes
Text
Towers of Hanoi in C#
Towers of Hanoi in C#
Towers of Hanoi or Tower of Brahma or Lucas’ Tower
Tower of Hanoi is a mathematical game or puzzle. It consists of three rods(towers), and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the…
View On WordPress
0 notes
Text
Solution #10
Solution behind the “Keep reading” thingy.
So there are a few different things to think about with Tower of Brahma/Hanoi puzzles, but the main thing for me is a kind of forward planning/goal-oriented thinking idea: what do I need to do, what do I need to do so I can do that, what do I need to do so I can do that, and so on.
With the regular tower, this manifests itself as the idea of recursion: as part of moving all the discs, I naturally have to move the biggest disc at some point, and to be able to do that I need a clear spot to move it to (because it’s too big to sit on top of anything else), so I have to stack all the other discs up somewhere. So in order to solve an n-disc tower, I must solve the (n-1)-disc tower, move the biggest disc, then solve the n-1 disc tower again.
If this is going over your head a bit, I’m going to link you to a nice video that Grimlock Master linked me to yesterday that explains this from the ground up: https://www.youtube.com/watch?v=2SUvWfNJSsM .
So, moving on to our actual puzzle. The idea is still the same, though: at some point, we’re going to have to move that biggest disc, the bottom blue one, and to do that, we’re going to need to get the other three discs to the currently empty peg. And to do that, we need to get the bigger red disc onto the empty peg. So our opening moves are:
(so we’re two moves in so far, for those keeping count for that eleven-move limit). All right, next we need to get the two smaller discs on to the bigger red disc, and that’s simple enough - it’s just a two-disc tower:
Five moves so far. Now we can move that biggest disc:
Six moves. And now it’s time to do the whole thing mirror-imaged in reverse to finish the job off:
Nine moves - happily that smaller blue disc is now exactly where we need it. And then it’s just a case of moving the bigger red disc to its new home, and putting the smaller red disc on top of it:
And we’re finished in eleven moves! Nicely done.
So here’s a neat extra thing about the towers of Brahma/Hanoi: you can map all possible legal states and moves out onto a version of the Sierpinski Gasket or Sierpinski Triangle. For those not familiar with this famous fractal: imagine the Triforce from Zelda. Now imagine each of those triangles is, itself, a Triforce. Now imagine each of the triangles that make these mini-Triforces up is also a Triforce. Now keep doing that forever. In short, this badboy.
But as I was saying, you can use this fractal to map out all these different positions the pieces can be in - and I’ve done just that! First, here’s the case with four discs.
I should explain the labelling here. I’ve called the three poles A, B and C, and coloured them red, green and blue. The sequence of four letters at each node in the network is the current peg of discs 1 through 4, 1 being the smallest, 4 being the largest. The three starting positions for the conventional puzzle are on the three corners of the triangle - I’ve drawn boxes around them in cyan. And if you follow the side edges of the triangle (one such edge highlighted in cyan), you’ll find the fastest routes to solve the conventional puzzle by getting the whole stack to a different pole. And you can see that for four discs, it’s fifteen moves. This is the shortest possible route, because it’s a straight line.
Now. The six possible start points for the “swapping” puzzle have magenta boxes drawn around them, and to get from one to another you have to follow, for instance, the route highlighted in magenta. Which, as you can see, is eleven moves long. It’s less obvious that this is the shortest route, but there aren’t many other ways around that big triangular hole in the middle - and if you count it out, you’ll see that going the other way takes fifteen moves.
What about the extension puzzle I suggested, with two sets of three discs that needed swapping? Well, okay, here’s the six disc version. You might want to download this one so you can get a proper look, it’s huge. This time, we have a sequence of six letters to show which peg we’re on.
Solving a regular tower of six, by the way, takes 63 moves (in fact, for n discs, solving the regular tower will take (2^n - 1) moves - the proof is left as an exercise to the reader). And you can see that the path this time for our “swap” puzzle takes 45 moves. I’d draw it all out, but it’d take quite a while and this blog post’s already really long.
Still, it’s remarkable what you can connect together across different fields.
#puzzle#maths#tower of hanoi#tower of brahma#strategic thinking#sierpinski gasket#sierpinski triangle
1 note
·
View note
Text
Prerequisites II - DS 101
Recursion
There are two ways code repetitive algorithms (solution that can be split into similar kind of sub-tasks), one is using loops/iteration and another is using recurrence. Recurrence is where an algorithm calls itself to solve the sub-problem which in turn solves the whole problem. Recursion either solves one part of the problem or it reduces the size of the problem. Therefore, there are two cases in a recursive algorithm. A base case, which solves a part of the problem and a general case which reduces the size of the problem.
Rules for designing a recurrsive algorithm:
Determine the base case
Determine the general case
Combine the base case and general cases into an algorithm
In combining the base case and the general case, we must make sure that each call must reduce the size of the problem and move towards the base case. When the base case is reached, it must terminate the recursion by providing the solution and should not call itself.
Limitations
Recursion works best when the algorithm uses a data structure which naturally exhibits a recursive structure (E.g. Tree data structure). Recursive solutions in most cases take an overhead of time and memory because they use calls. Therefore, they are slow than its iterative counterpart.
When not to use it?
Here are the scenarios when you should settle with a non-recursive solution:
If an algorithm or data structure is not naturally recursive.
If the recursive solution isn't shorter or more intuitive.
If the solution doesn't run in the required time and space limits.
When to use it?
As a general rule of thumb, recursion must be used only when the solution's efficiency is logarithmic. You must also check that the following basic requirements are fulfilled:
The problem asks for solving a large value and you can determine the small values (or the small value is already given).
There is a clear connection between the small values and the large value, such that combining all the small values can give the large value.
Towers of Hanoi
Towers of Hanoi is a classic recursion problem which is also an interesting case where the solution pattern is little different the usual problems.
The problem statement is that, there are 3 needles (let's assume A, B and C respectively), one is the source and one is the destination. Given disks on needle A, we need to move all of them to the destination needle (needle B in this case), using only one auxiliary (needle C), and must be done without breaking these rules:
Only one disk could be moved at a time.
A larger disk must never be placed on smaller one.
Solution Approach
The problem can be generalized into 3 cases:
Move the top n-1 disks from needle A (source) to needle C (auxiliary). General Case
Move 1 disk from needle A to needle B directly. Base Case
Now, move all the disks on needle C (n-1 disks) to needle B using needle A as the auxiliary. General Case
In this solution, we have 2 general cases and one base case (which solves a part of the problem by moving the disc). The general cases reduce the problem by assuming their own source, auxiliary & destination and move towards the base case of actual disk movement.
Pseudo code
def towers (total_disks, source, destination, auxiliary): print("Towers: ", source, destination, auxiliary) if total_disks is 1: print("Move from ", source, " to ", destination) else: towers(total_disks - 1, source, auxiliary, destination) print("Move from ", source, " to ", destination) towers(total_disks - 1, auxiliary, destination, source)
Calls & Output:
towers (3, A, B, C) towers (2, A, C, B) towers (1, A, B, C) # output: Move from A to B # output: Move from A to C towers (1, B, C, A) # output: Move from B to C # output: Move from A to B towers (3, C, B, C) towers (3, C, A, B) # output: Move from C to A # output: Move from C to B towers (3, A, B, C) # output: Move from A to B
1 note
·
View note
Link
Learn Complete C programming with loop, array, pointer, function, parameter, string, recursion, structure, file and more
What you’ll learn
if else statements, loop – while, for and do while loop with many examples.
Array – 1D and 2D, why we need them and how to use them effectively.
String in C – NULL terminated character arrays.
Writing function, parameter passing to function. Returning value from function.
storage class – auto, static, extern and register
Pointer – in depth understanding.
Relationship between arrays and pointers.
Array of pointers.
Command line arguments
Reading and writing with files, both text and binary.
Recursion – how it works, recursion vs iteration in depth discussion – Towers of Hanoi
Various string utilities – sprintf, strtok and many others
Function pointers
bitwise operators in C programming.
Requirements
No prerequisites, course is for absolute beginners.
Description
I have been teaching this course to the undergraduate engineering students for last 15 years in class room. This course is well designed and covered almost all the topics that one should know while learning C language. Will not only help the student to build a solid foundation on the topic but will boost their confidence to face technical interviews boldly.
The course contents are mostly video lectures. I would encourage absolute beginners to follow the lectures strictly in chronological manners, please start from the very first video and go to the next one only if you are done with the previous. However, though not recommended, but students with some previous knowledge could jump lectures if they are confident.
The course is structured basically for the new programmers who may not have any previous experience with any programming language. From the very basic to advanced topics. Simple program to complex one in step-by-step.
One should take this course to build a career as a programmer. Programming in C has been considered as foundation for any programming language. If one is confident with C, then can start learning any other language like PHP, C++ or Java.
Who this course is for:
This course is A-Z on C programming language, therefore, anyone can take this course, even absolute beginners in programming will face no problem doing this course
Any undergraduate student having C programming in curriculum
If you have previous experience in C programming or with any other programming language then this course is going to make your foundation more strong
Created by Shibaji Paul Last updated 8/2019 English English [Auto-generated]
Size: 1.85 GB
Download Now
https://ift.tt/2ZbF5fY.
The post C Programming Step by Step – Complete Tutorial For Beginners appeared first on Free Course Lab.
0 notes