Exemplo n.º 1
0
        /// <summary>
        /// Walks all of the possible paths from a specified origin point where each point is visited exactly once.
        /// </summary>
        /// <param name="current">The current point.</param>
        /// <param name="currentPath">The path that lead to the current point.</param>
        /// <param name="pathsWalked">A collection of all the paths walked so far.</param>
        /// <param name="pathsFromSources">The possible next paths and the distances to them from each location.</param>
        private static void WalkPaths(
            string current,
            Path currentPath,
            ICollection<Path> pathsWalked,
            IDictionary<string, IDictionary<string, int>> pathsFromSources)
        {
            if (currentPath.HasVisited(current))
            {
                // We've already been here so walk no further down this route
                return;
            }

            // Create a new path that is the scurrent path plus a step to this position
            int distanceFromPreviousToCurrent = pathsFromSources[currentPath.Current][current];

            Path nextPath = currentPath
                .Clone()
                .Visit(current, distanceFromPreviousToCurrent);

            pathsWalked.Add(nextPath);

            // Recursively continue down the path to the next possible destinations
            foreach (var next in pathsFromSources[nextPath.Current])
            {
                WalkPaths(next.Key, nextPath, pathsWalked, pathsFromSources);
            }
        }