Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Puzzles_04_TowerOfHanoi
- {
- class Program
- {
- static int conuter = 1;
- static void Main(string[] args)
- {
- int discs = 0;
- Console.Write("Please enter the number of discs : ");
- discs = Int32.Parse(Console.ReadLine());
- Tower(discs, "A", "B", "C");
- Console.ReadKey();
- }
- static void Tower(int n, string sourcePeg, string sparePeg, string destinationPeg)
- {
- if (n == 1)
- {
- Console.WriteLine(conuter + " " + sourcePeg + "→" + destinationPeg);
- conuter++;
- }
- else
- {
- Tower(n - 1, sourcePeg, destinationPeg, sparePeg);
- Tower(1, sourcePeg, sparePeg, destinationPeg);
- Tower(n - 1, sparePeg, sourcePeg, destinationPeg);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement