Guest User

Untitled

a guest
Oct 18th, 2019
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Puzzles_04_TowerOfHanoi
  8. {
  9. class Program
  10. {
  11. static int conuter = 1;
  12. static void Main(string[] args)
  13. {
  14.  
  15. int discs = 0;
  16. Console.Write("Please enter the number of discs : ");
  17. discs = Int32.Parse(Console.ReadLine());
  18. Tower(discs, "A", "B", "C");
  19. Console.ReadKey();
  20. }
  21.  
  22. static void Tower(int n, string sourcePeg, string sparePeg, string destinationPeg)
  23. {
  24. if (n == 1)
  25. {
  26. Console.WriteLine(conuter + " " + sourcePeg + "→" + destinationPeg);
  27. conuter++;
  28. }
  29. else
  30. {
  31. Tower(n - 1, sourcePeg, destinationPeg, sparePeg);
  32. Tower(1, sourcePeg, sparePeg, destinationPeg);
  33. Tower(n - 1, sparePeg, sourcePeg, destinationPeg);
  34.  
  35. }
  36. }
  37.  
  38. }
  39. }
RAW Paste Data