03 Jul

C# Towers Of Hanoi Problem Implementation

This example contains a recursive solution for the Towers of Hanoi problem.

For more information about Hanoi Tower:
http://en.wikipedia.org/wiki/Tower_of_Hanoi

Usage:

            MoveDisc(3, 1, 3, 2);

            //Output:
            //Index of 20 value in list is 4
            //Move disk 1 from tower 1 to tower 3
            //Move disk 2 from tower 1 to tower 2
            //Move disk 1 from tower 3 to tower 2
            //Move disk 3 from tower 1 to tower 3
            //Move disk 1 from tower 2 to tower 1
            //Move disk 2 from tower 2 to tower 3
            //Move disk 1 from tower 1 to tower 3
        public void MoveDisc(int n, int from, int to, int other)
        {
            if (n > 0)
            {
                MoveDisc(n - 1, from, other, to);
                Console.WriteLine("Move disk {0} from tower {1} to tower {2}", n, from, to);
                MoveDisc(n - 1, other, to, from);
            }
        }

2 thoughts on “C# Towers Of Hanoi Problem Implementation

Leave a Reply

Your email address will not be published. Required fields are marked *