|
namespace Recursion
{
//Tower of Hanoi Example
//Tower of Hanoi problem: Given three pegs, one with a set of N disks of increasing size,
//determine the minimum (optimal) number of steps it take to move all the disks from their
//initial position to another peg without placing a larger disk on top of a smaller one.
class TowerofHanoi
{
static int depth;
public TowerofHanoi() { }
public int DoDiskMovements(int disks)
{
depth++;
if (depth == 1)
{
if (disks <= 0)
return 0;
}
if (disks == 1 )
{
return 1;
}
else
{
return 2 * DoDiskMovements(disks - 1) + 1;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Working as Senior Developer in US based MNC. Interested in R&D, writing/reading techinical articles, sci-fi movies, playing synthesizer.