Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Algorithms
- {
- class Program
- {
- private static int steps = 0;
- private static Stack<int> source;
- private static Stack<int> destination;
- private static Stack<int> spare;
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- var range = Enumerable.Range(1, n).Reverse();
- source = new Stack<int>(range);
- destination = new Stack<int>();
- spare = new Stack<int>();
- PrintRods();
- MoveDisks(n, source, spare, destination);
- }
- private static void MoveDisks(int bottomDisk, Stack<int> sourceRod, Stack<int> spareRod, Stack<int> destinationRod)
- {
- if (bottomDisk == 1)
- {
- steps++;
- destinationRod.Push(sourceRod.Pop());
- Console.WriteLine($"Step #{steps}: Moved disk");
- PrintRods();
- return;
- }
- MoveDisks(bottomDisk - 1, sourceRod, destinationRod, spareRod);
- destinationRod.Push(sourceRod.Pop());
- steps++;
- Console.WriteLine($"Step #{steps}: Moved disk");
- PrintRods();
- MoveDisks(bottomDisk - 1, spareRod, sourceRod, destinationRod);
- }
- private static void PrintRods()
- {
- Console.WriteLine($"Source: {string.Join(", ", source.Reverse())}");
- Console.WriteLine($"Destination: {string.Join(", ", destination.Reverse())}");
- Console.WriteLine($"Spare: {string.Join(", ", spare.Reverse())}");
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement