private ResultPath GetResultPath(InputTriangle inputTriangle, int[][] leftSums, int[][] rightSums, int[][] bestSums)
        {
            var resultPath = new ResultPath {
                Nodes = new List <Node>()
            };

            if (bestSums[0][0] == NoAllowedStep)
            {
                return(resultPath);
            }

            resultPath.PathSum = bestSums[0][0] + inputTriangle.Levels[0][0];

            var bestColumn = 0;

            for (int level = 0; level < inputTriangle.Levels.Length; level++)
            {
                resultPath.Nodes.Add(new Node
                {
                    Column = bestColumn,
                    Value  = inputTriangle.Levels[level][bestColumn]
                });
                if (level < inputTriangle.Levels.Length - 1 &&
                    bestSums[level][bestColumn] == rightSums[level][bestColumn])
                {
                    bestColumn++;
                }
            }

            return(resultPath);
        }
示例#2
0
        private void FillResultPath(ResultPath resultPath, Node bestNode)
        {
            if (!resultPath.Nodes.Any())
            {
                resultPath.PathSum = GetMaxFromTwo(bestNode.GoLeftSum, bestNode.GoRightSum) + bestNode.Value;
            }

            if (bestNode == null)
            {
                return;
            }

            resultPath.Nodes.Add(bestNode);

            if (bestNode.GoLeftSum != null && bestNode.GoRightSum == null ||
                bestNode.GoLeftSum >= bestNode.GoRightSum)
            {
                FillResultPath(resultPath, bestNode.LeftChild);
            }
            else if (bestNode.GoRightSum != null && bestNode.GoLeftSum == null ||
                     bestNode.GoRightSum >= bestNode.GoLeftSum)
            {
                FillResultPath(resultPath, bestNode.RightChild);
            }
        }
        public void DisplaysTenRow_when_TenRowTriangleGiven()
        {
            var inputTriangle = new InputTriangle
            {
                Levels = new int[][]
                {
                    new int[] { 1 },
                    new int[] { 2, 3 },
                    new int[] { 4, 5, 6 }
                }
            };

            var resultPath = new ResultPath {
                Nodes = new List <Node> {
                    new Node {
                        Value = 7
                    }, new Node {
                        Value = 2
                    }, new Node {
                        Value = 5
                    }
                }, PathSum = 14, SolvingTime = TimeSpan.FromSeconds(3)
            };

            _sut.Display(inputTriangle, resultPath);

            _consoleWrapperMock.Verify(x => x.Write("  1"), Times.Once);
            _consoleWrapperMock.Verify(x => x.Write("  2"), Times.Once);
            _consoleWrapperMock.Verify(x => x.Write("  3"), Times.Once);
            _consoleWrapperMock.Verify(x => x.Write("  4"), Times.Once);
            _consoleWrapperMock.Verify(x => x.Write("  5"), Times.Once);
            _consoleWrapperMock.Verify(x => x.Write("  6"), Times.Once);
            _consoleWrapperMock.Verify(x => x.Write(It.Is <string>(y => y.Contains("maximum"))), Times.Once);
            _consoleWrapperMock.Verify(x => x.WriteLine(It.Is <string>(y => y.Contains("quit"))), Times.Once);
        }
示例#4
0
        private static void RepairResultPath()
        {
            if (ResultPath.Contains('.'))
            {
                var indexOfDot = ResultPath.LastIndexOf('.');
                ResultPath = ResultPath.Substring(0, ResultPath.Length - (ResultPath.Length - indexOfDot));
            }

            ResultPath = ResultPath + "." + OutputFormat.ToString().ToLower();
        }
示例#5
0
        public ResultPath Solve(InputTriangle inputTriangle)
        {
            var topNode = _graphCreator.Create(inputTriangle);

            VisitNode(topNode, inputTriangle.Levels.Length);

            var resultPath = new ResultPath {
                Nodes = new List <Node>()
            };

            if (topNode.GoLeftSum != null || topNode.GoRightSum != null)
            {
                FillResultPath(resultPath, topNode);
            }

            return(resultPath);
        }
示例#6
0
文件: Dijkstra.cs 项目: sheviak/Train
        /// <summary>
        /// Формирование пути
        /// </summary>
        /// <param name="startVertex">Начальная вершина</param>
        /// <param name="endVertex">Конечная вершина</param>
        /// <returns>Путь</returns>
        ResultPath GetPath(GraphVertex startVertex, GraphVertex endVertex)
        {
            var result = new ResultPath();

            result.Path.Add(endVertex.ToString());

            while (startVertex != endVertex)
            {
                var vertexInfo = GetVertexInfo(endVertex);
                endVertex = vertexInfo.PreviousVertex;

                result.Weight.Insert(0, vertexInfo.weight);
                result.Train.Insert(0, vertexInfo.train);
                result.Path.Insert(0, endVertex.ToString()); // + path;
            }

            return(result);
        }
        public void DisplaysOneRow_when_SimplestTriangleGiven()
        {
            var inputTriangle = new InputTriangle
            {
                Levels = new int[][]
                {
                    new int[] { 7 }
                }
            };

            var resultPath = new ResultPath {
                Nodes = new List <Node> {
                    new Node {
                        Value = 7
                    }
                }, PathSum = 7, SolvingTime = TimeSpan.FromSeconds(2)
            };

            _sut.Display(inputTriangle, resultPath);

            _consoleWrapperMock.Verify(x => x.Write("  7"), Times.Once);
            _consoleWrapperMock.Verify(x => x.Write(It.Is <string>(y => y.Contains("maximum"))), Times.Once);
            _consoleWrapperMock.Verify(x => x.WriteLine(It.Is <string>(y => y.Contains("quit"))), Times.Once);
        }
示例#8
0
        public void Execute()
        {
            ResultPath.Clear();
            Location current = null;
            var      start   = new Location {
                X = From.X, Y = From.Y
            };
            var target = new Location {
                X = To.X, Y = To.Y
            };
            var openList   = new List <Location>();
            var closedList = new List <Location>();
            int g          = 0;

            openList.Add(start);
            while (openList.Count > 0)
            {
                // get the square with the lowest F score
                var lowest = openList.Min(l => l.F);
                current = openList.First(l => l.F == lowest);

                // add the current square to the closed list
                closedList.Add(current);


                // remove it from the open list
                openList.Remove(current);

                // if we added the destination to the closed list, we've found a path
                if (closedList.FirstOrDefault(l => l.X == target.X && l.Y == target.Y) != null)
                {
                    break;
                }

                var adjacentSquares = GetWalkableAdjacentSquares(current.X, current.Y, map.Map);
                g++;

                foreach (var adjacentSquare in adjacentSquares)
                {
                    // if this adjacent square is already in the closed list, ignore it
                    if (closedList.FirstOrDefault(l => l.X == adjacentSquare.X &&
                                                  l.Y == adjacentSquare.Y) != null)
                    {
                        continue;
                    }

                    // if it's not in the open list...
                    if (openList.FirstOrDefault(l => l.X == adjacentSquare.X &&
                                                l.Y == adjacentSquare.Y) == null)
                    {
                        // compute its score, set the parent
                        adjacentSquare.G      = g;
                        adjacentSquare.H      = ComputeHScore(adjacentSquare.X, adjacentSquare.Y, target.X, target.Y);
                        adjacentSquare.F      = adjacentSquare.G + adjacentSquare.H;
                        adjacentSquare.Parent = current;

                        // and add it to the open list
                        openList.Insert(0, adjacentSquare);
                    }
                    else
                    {
                        // test if using the current G score makes the adjacent square's F score
                        // lower, if yes update the parent because it means it's a better path
                        if (g + adjacentSquare.H < adjacentSquare.F)
                        {
                            adjacentSquare.G      = g;
                            adjacentSquare.F      = adjacentSquare.G + adjacentSquare.H;
                            adjacentSquare.Parent = current;
                        }
                    }
                }
            }

            // assume path was found; let's show it
            while (current != null)
            {
                ResultPath.Add(current);
                current = current.Parent;
            }

            // end
        }
示例#9
0
 private string GetResultPath()
 {
     return($"{ResultFormat} {ResultPath.DoubleQuoteIfNeeded()}");
 }