コード例 #1
0
        public static HexCell[] Path(int from, int to, HexCell[] cells)
        {
            SimplePriorityQueue <HexCell> frontier = new SimplePriorityQueue <HexCell>();
            Dictionary <HexCell, HexCell> visited  = new Dictionary <HexCell, HexCell>();
            Dictionary <HexCell, float>   costAcc  = new Dictionary <HexCell, float>();

            HexCell start = cells[from];
            HexCell goal  = cells[to];

            VisitCell(start, null, 0, ref frontier, ref visited);
            costAcc[start] = 0;

            while (frontier.Count > 0)
            {
                HexCell curr = frontier.Dequeue();

                if (curr == goal)
                {
                    break;
                }

                foreach (HexCell cell in curr.GetAllNeighbors())
                {
                    float nextCost = costAcc[curr] + cell.MoveCost;

                    if (!visited.ContainsKey(cell) || nextCost < costAcc[cell])
                    {
                        costAcc[cell] = nextCost + HeuristicCost(cell, goal);

                        VisitCell(cell, curr, nextCost, ref frontier, ref visited);
                    }
                }
            }
            return(PathfindingBase.ConstructPath(start, goal, visited));
        }
コード例 #2
0
        public static HexCell[] Path(int from, int to, HexCell[] cells)
        {
            Queue <HexCell> frontier = new Queue <HexCell>();
            Dictionary <HexCell, HexCell> visited = new Dictionary <HexCell, HexCell>();

            HexCell start = cells[from];
            HexCell goal  = cells[to];

            VisitCell(start, null, ref frontier, ref visited);

            while (frontier.Count > 0)
            {
                HexCell curr = frontier.Dequeue();

                if (curr == goal)
                {
                    break;
                }

                foreach (HexCell cell in curr.GetAllNeighbors())
                {
                    if (!visited.ContainsKey(cell))
                    {
                        VisitCell(cell, curr, ref frontier, ref visited);
                    }
                }
            }
            return(PathfindingBase.ConstructPath(start, goal, visited));
        }