コード例 #1
0
    public static SearchResult BestFirstSearch(Grid grid, Grid.Point startPos, Grid.Point endPos)
    {
        FakePriorityQueue <Grid.Point>      queue       = new FakePriorityQueue <Grid.Point>();
        Dictionary <Grid.Point, float>      distanceMap = new Dictionary <Grid.Point, float>();
        Dictionary <Grid.Point, Grid.Point> visitedMap  = new Dictionary <Grid.Point, Grid.Point>();

        queue.Enqueue(startPos, 0);
        distanceMap.Add(startPos, 0);
        visitedMap.Add(startPos, null);

        while (!queue.Empty)
        {
            Grid.Point current = queue.Dequeue();
            if (current.Equals(endPos))
            {
                return(new SearchResult
                {
                    Path = GeneratePath(visitedMap, current),
                    Visited = new List <Grid.Point>(visitedMap.Keys)
                });
            }

            foreach (Grid.Point neighbour in grid.GetAdjacentCells(current))
            {
                if (!visitedMap.ContainsKey(neighbour))
                {
                    float priority = Heuristic(endPos, neighbour);
                    queue.Enqueue(neighbour, priority);
                    visitedMap.Add(neighbour, current);
                    distanceMap.Add(neighbour, distanceMap[current] + grid.GetCostOfEnteringCell(neighbour));
                }
            }
        }
        return(new SearchResult());
    }
コード例 #2
0
    public static SearchResult DijkstraPriority(Grid grid, Grid.Point startPos, Grid.Point endPos)
    {
        FakePriorityQueue <Grid.Point>      queue       = new FakePriorityQueue <Grid.Point>();
        Dictionary <Grid.Point, float>      distanceMap = new Dictionary <Grid.Point, float>();
        Dictionary <Grid.Point, Grid.Point> visitedMap  = new Dictionary <Grid.Point, Grid.Point>();

        queue.Enqueue(startPos, 0);

        distanceMap.Add(startPos, 0);
        visitedMap.Add(startPos, null);

        while (!queue.Empty)
        {
            Grid.Point current = queue.Dequeue();
            if (current.Equals(endPos))
            {
                return(new SearchResult
                {
                    Path = GeneratePath(visitedMap, current),
                    Visited = new List <Grid.Point>(visitedMap.Keys)
                });
            }
            foreach (Grid.Point adj in grid.GetAdjacentCells(current))
            {
                float newDist = distanceMap[current] + grid.GetCostOfEnteringCell(adj);
                if (!distanceMap.ContainsKey(adj) || newDist < distanceMap[adj])
                {
                    distanceMap[adj] = newDist;
                    visitedMap[adj]  = current;
                    queue.Enqueue(adj, newDist);
                }
            }
        }
        return(new SearchResult());
    }
コード例 #3
0
    public static SearchResult Dijkstra(Grid grid, Grid.Point startPos, Grid.Point endPos)
    {
        List <Grid.Point> unfinishedVertices            = new List <Grid.Point>();
        Dictionary <Grid.Point, float>      distanceMap = new Dictionary <Grid.Point, float>();
        Dictionary <Grid.Point, Grid.Point> visitedMap  = new Dictionary <Grid.Point, Grid.Point>();

        unfinishedVertices.Add(startPos);

        distanceMap.Add(startPos, 0);
        visitedMap.Add(startPos, null);

        while (unfinishedVertices.Count > 0)
        {
            Grid.Point vertex = GetClosestVertex(unfinishedVertices, distanceMap);
            unfinishedVertices.Remove(vertex);
            if (vertex.Equals(endPos))
            {
                return(new SearchResult
                {
                    Path = GeneratePath(visitedMap, vertex),
                    Visited = new List <Grid.Point>(visitedMap.Keys)
                });
            }
            foreach (Grid.Point adj in grid.GetAdjacentCells(vertex))
            {
                if (!visitedMap.ContainsKey(adj))
                {
                    unfinishedVertices.Add(adj);
                }
                float adjDist = distanceMap.ContainsKey(adj) ? distanceMap[adj] : int.MaxValue;
                float vDist   = distanceMap.ContainsKey(vertex) ? distanceMap[vertex] : int.MaxValue;
                if (adjDist > vDist + grid.GetCostOfEnteringCell(adj))
                {
                    if (distanceMap.ContainsKey(adj))
                    {
                        distanceMap[adj] = vDist + grid.GetCostOfEnteringCell(adj);
                    }
                    else
                    {
                        distanceMap.Add(adj, vDist + grid.GetCostOfEnteringCell(adj));
                    }
                    if (visitedMap.ContainsKey(adj))
                    {
                        visitedMap[adj] = vertex;
                    }
                    else
                    {
                        visitedMap.Add(adj, vertex);
                    }
                }
            }
        }
        return(new SearchResult());
    }
コード例 #4
0
    public static SearchResult AStarSearch(Grid grid, Grid.Point startPos, Grid.Point endPos)
    {
        List <Grid.Point> openSet = new List <Grid.Point>();
        Dictionary <Grid.Point, float>      costSoFar   = new Dictionary <Grid.Point, float>();
        Dictionary <Grid.Point, float>      priorityMap = new Dictionary <Grid.Point, float>();
        Dictionary <Grid.Point, Grid.Point> cameFrom    = new Dictionary <Grid.Point, Grid.Point>();

        openSet.Add(startPos);
        priorityMap.Add(startPos, 0);
        costSoFar.Add(startPos, 0);
        cameFrom.Add(startPos, null);

        while (openSet.Count > 0)
        {
            Grid.Point current = GetClosestVertex(openSet, priorityMap);
            openSet.Remove(current);
            if (current.Equals(endPos))
            {
                return(new SearchResult
                {
                    Path = GeneratePath(cameFrom, current),
                    Visited = new List <Grid.Point>(cameFrom.Keys)
                });
            }

            foreach (Grid.Point neighbour in grid.GetAdjacentCells(current))
            {
                float newCost = costSoFar[current] + grid.GetCostOfEnteringCell(neighbour);
                if (!costSoFar.ContainsKey(neighbour) || newCost < costSoFar[neighbour])
                {
                    costSoFar[neighbour] = newCost;

                    float priority = newCost + Heuristic(endPos, neighbour);
                    openSet.Add(neighbour);
                    priorityMap[neighbour] = priority;

                    cameFrom[neighbour] = current;
                }
            }
        }
        return(new SearchResult());
    }
コード例 #5
0
    public static SearchResult BreadthFirstSearch(Grid grid, Grid.Point startPos, Grid.Point endPos)
    {
        if (startPos.Equals(endPos))
        {
            return(new SearchResult());
        }

        Dictionary <Grid.Point, Grid.Point> visitedMap = new Dictionary <Grid.Point, Grid.Point>();

        Queue <Grid.Point> queue = new Queue <Grid.Point>();

        queue.Enqueue(startPos);

        while (queue.Count > 0)
        {
            Grid.Point node = queue.Dequeue();

            foreach (Grid.Point adj in grid.GetAdjacentCells(node))
            {
                if (!visitedMap.ContainsKey(adj))
                {
                    visitedMap.Add(adj, node);
                    queue.Enqueue(adj);

                    if (adj.Equals(endPos))
                    {
                        return(new SearchResult
                        {
                            Path = GeneratePath(visitedMap, adj),
                            Visited = new List <Grid.Point>(visitedMap.Keys)
                        });
                    }
                }
            }
            if (!visitedMap.ContainsKey(node))
            {
                visitedMap.Add(node, null);
            }
        }
        return(new SearchResult());
    }
コード例 #6
0
    public static SearchResult DepthFirstSearch(Grid grid, Grid.Point startPos, Grid.Point endPos)
    {
        if (startPos.Equals(endPos))
        {
            return(new SearchResult());
        }

        Dictionary <Grid.Point, Grid.Point> visitedMap = new Dictionary <Grid.Point, Grid.Point>();

        Stack <Grid.Point> stack = new Stack <Grid.Point>();

        stack.Push(startPos);

        while (stack.Count > 0)
        {
            Grid.Point node = stack.Pop();

            foreach (Grid.Point adj in grid.GetAdjacentCells(node))
            {
                if (!visitedMap.ContainsKey(adj))
                {
                    visitedMap.Add(adj, node);
                    stack.Push(adj);

                    if (adj.Equals(endPos))
                    {
                        return(new SearchResult
                        {
                            Path = GeneratePath(visitedMap, adj),
                            Visited = new List <Grid.Point>(visitedMap.Keys)
                        });
                    }
                }
            }
            if (!visitedMap.ContainsKey(node))
            {
                visitedMap.Add(node, null);
            }
        }
        return(new SearchResult());
    }
コード例 #7
0
    public static SearchResult BestFirstSearch2(Grid grid, Grid.Point startPos, Grid.Point endPos)
    {
        List <Grid.Point> openSet = new List <Grid.Point>();
        Dictionary <Grid.Point, float>      distanceMap = new Dictionary <Grid.Point, float>();
        Dictionary <Grid.Point, float>      priorityMap = new Dictionary <Grid.Point, float>();
        Dictionary <Grid.Point, Grid.Point> visitedMap  = new Dictionary <Grid.Point, Grid.Point>();

        openSet.Add(startPos);
        priorityMap.Add(startPos, 0);
        distanceMap.Add(startPos, 0);
        visitedMap.Add(startPos, null);

        while (openSet.Count > 0)
        {
            Grid.Point current = GetClosestVertex(openSet, priorityMap);
            openSet.Remove(current);
            if (current.Equals(endPos))
            {
                return(new SearchResult
                {
                    Path = GeneratePath(visitedMap, current),
                    Visited = new List <Grid.Point>(visitedMap.Keys)
                });
            }

            foreach (Grid.Point neighbour in grid.GetAdjacentCells(current))
            {
                if (!visitedMap.ContainsKey(neighbour))
                {
                    float priority = Heuristic(endPos, neighbour);
                    openSet.Add(neighbour);
                    visitedMap.Add(neighbour, current);
                    priorityMap.Add(neighbour, priority);
                    distanceMap.Add(neighbour, distanceMap[current] + grid.GetCostOfEnteringCell(neighbour));
                }
            }
        }
        return(new SearchResult());
    }
コード例 #8
0
    public static SearchResult AStarSearchPriority(Grid grid, Grid.Point startPos, Grid.Point endPos)
    {
        FakePriorityQueue <Grid.Point>      queue     = new FakePriorityQueue <Grid.Point>();
        Dictionary <Grid.Point, float>      costSoFar = new Dictionary <Grid.Point, float>();
        Dictionary <Grid.Point, Grid.Point> cameFrom  = new Dictionary <Grid.Point, Grid.Point>();

        queue.Enqueue(startPos, 0);
        costSoFar[startPos] = 0;
        cameFrom[startPos]  = null;

        while (!queue.Empty)
        {
            Grid.Point current = queue.Dequeue();
            if (current.Equals(endPos))
            {
                return(new SearchResult
                {
                    Path = GeneratePath(cameFrom, current),
                    Visited = new List <Grid.Point>(cameFrom.Keys)
                });
            }
            foreach (Grid.Point neighbour in grid.GetAdjacentCells(current))
            {
                float newCost = costSoFar[current] + grid.GetCostOfEnteringCell(neighbour);
                if (!costSoFar.ContainsKey(neighbour) || newCost < costSoFar[neighbour])
                {
                    costSoFar[neighbour] = newCost;

                    float priority = newCost + Heuristic(endPos, neighbour);
                    queue.Enqueue(neighbour, priority);

                    cameFrom[neighbour] = current;
                }
            }
        }
        return(new SearchResult());
    }