public void BuildGraph() { AdjacencyList = new Dictionary <Point, List <Point> >(); for (int y = 0; y < BoundingRect.Height; y++) { for (int x = 0; x < BoundingRect.Width; x++) { Point p = new Point(x, y); if (Walls.Contains(p) || Creatures.ContainsKey(p)) { continue; } AdjacencyList.Add(p, new List <Point>()); List <Point> adjacentList = Fifteen.AdjacentPoints(p); foreach (Point ap in adjacentList) { if (Walls.Contains(ap) || Creatures.ContainsKey(ap)) { continue; } AdjacencyList[p].Add(ap); } } } }
public (Point, int) GetPathDistPoint_AStar(Point from, Point to) { List <Point> closedSet = new List <Point>(); List <Point> openSet = new List <Point>() { from }; Dictionary <Point, Point> cameFrom = new Dictionary <Point, Point>(); Dictionary <Point, int> gScore = new Dictionary <Point, int>() { { from, 0 } }; Dictionary <Point, int> fScore = new Dictionary <Point, int>() { { from, Fifteen.ManhattanDist(from, to) } }; bool found = false; while (openSet.Any()) { Point current = openSet.OrderBy(p => fScore[p]).First(); if (current.Equals(to)) { found = true; break; } openSet.Remove(current); closedSet.Add(current); foreach (Point p in Fifteen.AdjacentPoints(current).OrderBy(p => p)) { if (Walls.Contains(p) || Creatures.ContainsKey(p)) { continue; } if (closedSet.Contains(p)) { continue; } int tentativeGScore = gScore[current] + 1; if (!openSet.Contains(p)) { openSet.Add(p); } else if (tentativeGScore >= gScore[p]) { continue; } cameFrom[p] = current; gScore[p] = tentativeGScore; fScore[p] = Fifteen.ManhattanDist(p, to); //fScore[p] = tentativeGScore + Fifteen.ManhattanDist(p, to) + p.X + p.Y; } } if (found) { int dist = 0; Point current = to; List <Point> totalPath = new List <Point>() { current }; while (cameFrom.ContainsKey(current)) { dist++; current = cameFrom[current]; totalPath.Add(current); } return(totalPath[totalPath.Count - 2], dist); } else { return(null, int.MaxValue); } }
public (Point, int) GetPathDistPoint_BFS(Point from, HashSet <Point> spots) { Dictionary <int, List <Point> > distancePoints = new Dictionary <int, List <Point> >() { { 0, new List <Point> { from } } }; Queue <Point> openSet = new Queue <Point>(); HashSet <Point> closedSet = new HashSet <Point>(); Dictionary <Point, Point> cameFrom = new Dictionary <Point, Point>(); cameFrom[from] = null; openSet.Enqueue(from); bool found = false; Point closestSpot = null; while (openSet.Any()) { Point currentRoot = openSet.Dequeue(); if (spots.Contains(currentRoot)) { found = true; } foreach (Point p in Fifteen.AdjacentPoints(currentRoot).OrderBy(p => p)) { if (Walls.Contains(p) || Creatures.ContainsKey(p)) { continue; } if (closedSet.Contains(p)) { continue; } if (!openSet.Contains(p)) { cameFrom[p] = currentRoot; openSet.Enqueue(p); } } closedSet.Add(currentRoot); } if (found) { spots.RemoveWhere(p => !cameFrom.ContainsKey(p)); Dictionary <int, List <Point> > results = new Dictionary <int, List <Point> >(); foreach (Point spot in spots) { int dist = 0; Point current = spot; List <Point> totalPath = new List <Point>() { current }; while (current != null && cameFrom.ContainsKey(current)) { dist++; current = cameFrom[current]; totalPath.Add(current); } if (!results.ContainsKey(dist)) { results.Add(dist, new List <Point>()); } results[dist].Add(totalPath[totalPath.Count - 3]); } int minDist = results.Keys.Min(); Point minPoint = results[minDist].OrderBy(p => p).First(); return(minPoint, minDist); } else { return(null, int.MaxValue); } }