Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Task 10*
- * We are given numbers N and M and the following operations:
- * N = N+1
- * N = N+2
- * N = N*2
- * Write a program that finds the shortest sequence of operations
- * from the list above that starts from N and finishes in M.
- * Hint: use a queue.
- * Example: N = 5, M = 16
- * Sequence: 5 ---> 6 ---> 8 ---> 16
- */
- namespace Task10Version2ShortestSequenceOfOperation.Common
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class ShortestPathFinder
- {
- private readonly Queue<TreeNode<int>> holder = new Queue<TreeNode<int>>();
- private readonly List<TreeNode<int>> results = new List<TreeNode<int>>();
- private readonly List<string> pathsWithoutDublicates = new List<string>();
- /// <summary>
- /// Initializes a new instance of the <see cref="ShortestPathFinder" /> class.
- /// </summary>
- /// <param name="start">The start of path.</param>
- /// <param name="stop">The stop of path.</param>
- public ShortestPathFinder(int start, int stop)
- {
- TreeNode<int> root = new TreeNode<int>(start);
- this.CreateTree(start, stop, root, this.holder, -1, this.results);
- }
- /// <summary>
- /// Printing all shortest paths.
- /// </summary>
- public void PrintShortestPaths()
- {
- this.ProcessPaths();
- foreach (var path in this.pathsWithoutDublicates)
- {
- Console.WriteLine(path);
- Console.WriteLine();
- }
- }
- /// <summary>
- /// Recursive function to fill paths tree.
- /// </summary>
- private void CreateTree(int start, int stop, TreeNode<int> currentNode, Queue<TreeNode<int>> holder, int treeLevelOfStop, List<TreeNode<int>> results)
- {
- if (treeLevelOfStop == -1 || currentNode.NodeTreeLevel < treeLevelOfStop)
- {
- TreeNode<int> firstChildToAdd = new TreeNode<int>(currentNode.Value * 2);
- TreeNode<int> secondChildToAdd = new TreeNode<int>(currentNode.Value + 2);
- TreeNode<int> thirdChildToAdd = new TreeNode<int>(currentNode.Value + 1);
- if (firstChildToAdd.Value == stop ||
- secondChildToAdd.Value == stop ||
- thirdChildToAdd.Value == stop)
- {
- treeLevelOfStop = currentNode.NodeTreeLevel + 1;
- }
- if (currentNode.Value > 0 &&
- firstChildToAdd.Value > start && firstChildToAdd.Value <= stop)
- {
- currentNode.AddChild(firstChildToAdd);
- }
- if (secondChildToAdd.Value > start && secondChildToAdd.Value <= stop)
- {
- currentNode.AddChild(secondChildToAdd);
- }
- if (thirdChildToAdd.Value > start && thirdChildToAdd.Value <= stop)
- {
- currentNode.AddChild(thirdChildToAdd);
- }
- for (int i = 0; i < currentNode.CountChildren(); i++)
- {
- TreeNode<int> child = currentNode.GetChild(i);
- if (child.Value == stop)
- {
- results.Add(child);
- }
- holder.Enqueue(child);
- }
- currentNode = holder.Dequeue();
- this.CreateTree(start, stop, currentNode, holder, treeLevelOfStop, results);
- }
- }
- /// <summary>
- /// Make paths from result and remove dublicates
- /// </summary>
- private void ProcessPaths()
- {
- for (int i = 0; i < this.results.Count; i++)
- {
- TreeNode<int> result = this.results[i];
- Stack<int> path = new Stack<int>();
- while (result != null)
- {
- path.Push(result.Value);
- result = result.Parent;
- }
- string finalPath = string.Join(" -> ", path);
- if (!this.pathsWithoutDublicates.Contains(finalPath))
- {
- this.pathsWithoutDublicates.Add(finalPath);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement