public List<Node> ConnectedNodes(Node current, Location targetLoc) { //Returns the nodes around a certain node, depending on whether those surrounding nodes are dirt, water or unknown //Input requires location of node, with the distance that has been traversed so far int x = current.X; int y = current.Y; //Check left, right, up and down List<Node> connected = new List<Node>(); //Check left x--; if (x < 0) x = w - 1; //For wrapping around the map if (map[x, y].IsPassable) connected.Add(new Node(x, y, DistanceToLocation(x, y, targetLoc) + current.DistanceTraversed + 1, current.DistanceTraversed + 1)); //new node has x, y, tentative f score, tentative g score //Check right x = current.X + 1; if (x == w) x = 0; //For wrapping around the map if (map[x, y].IsPassable) connected.Add(new Node(x, y, DistanceToLocation(x, y, targetLoc) + current.DistanceTraversed + 1, current.DistanceTraversed + 1)); //Check up x = current.X; y--; if (y < 0) y = h - 1; //For wrapping around the map if (map[x, y].IsPassable) connected.Add(new Node(x, y, DistanceToLocation(x, y, targetLoc) + current.DistanceTraversed + 1, current.DistanceTraversed + 1)); //Check down y = current.Y + 1; if (y == h) y = h - 1; //For wrapping around the map if (map[x, y].IsPassable) connected.Add(new Node(x, y, DistanceToLocation(x, y, targetLoc) + current.DistanceTraversed + 1, current.DistanceTraversed + 1)); return connected; }
public void CameFrom(Node node) { cameFrom = node; }
public double CompareTo(Node n) { //Compare the distanceToTarget with another node return tentative_f_score - n.tentative_f_score; }
public List<char> direction_algor_A(Location loc1, Location loc2) { //Открытий список вершин по которым идет поиск IDictionary<Location, Node > OpenList = new Dictionary<Location, Node> (); //Закрытий список вершин поиск завершен IDictionary<Location, Node > CloseList = new Dictionary<Location, Node> (); //Оптимизация поиска по массиву открытих вершин IDictionary<int,List<Location >> Tree = new Dictionary<int, List<Location>> (); List<char > directions = new List<char> (); //начальная точка CloseList.Add (loc1, new Node (loc1, new Location (0, distance (loc1, loc2)))); Location loc = loc1; bool finish = true; DateTime start = DateTime.Now; int head = int.MaxValue; while (finish) { foreach (char c in Ants.Aim.Keys) { Location newLoc = destination (loc, c); if (unoccupied (newLoc) && !CloseList.Keys.Contains (newLoc)) { Location size = new Location (CloseList [loc].Size.row + 1, distance (newLoc, loc2)); int f = size.row + size.col; if (OpenList.ContainsKey (newLoc)) { if (size.row < OpenList [newLoc].Size.row) { //Смена значание Node требует смены индекса в дереве int ff = OpenList [newLoc].Size.row + OpenList [newLoc].Size.col; Tree [ff].Remove (newLoc); if (Tree [ff].Count == 0) Tree.Remove (ff); if (ff == head) { head = int.MaxValue; foreach (var item in Tree.Keys) { if (item < head) head = item; } } OpenList [newLoc] = new Node (loc, size); if (f < head) head = f; if (!Tree.ContainsKey (f)) Tree.Add (f, new List<Location> ()); Tree [f].Add (newLoc); } } else { OpenList.Add (newLoc, new Node (loc, size)); if (f < head) head = f; if (!Tree.ContainsKey (f)) Tree.Add (f, new List<Location> ()); Tree [f].Add (newLoc); } } } if (Tree.Keys.Count > 0) { //loc = Tree [head] [Tree[head].Count - 1]; loc = Tree [head] [0]; CloseList.Add (loc, OpenList [loc]); OpenList.Remove (loc); Tree [head].Remove (loc); if (Tree [head].Count == 0) { Tree.Remove (head); head = int.MaxValue; foreach (var item in Tree.Keys) { if (item < head) head = item; } } } else { break; } if ((TimeRemaining < 30) || ((DateTime.Now - start).Milliseconds > TurnTime / 10)) //return new List<char> (); break; if (CloseList.ContainsKey (loc2)) finish = false; } Location find = loc1; Location old; if (CloseList.ContainsKey (loc2)) { //Точка достижима find = loc2; } else { //Точка не достижима int h = int.MaxValue; foreach (var item in CloseList.Keys) { if ((CloseList [item].Size.col < h) && ((item.row != loc1.row) || (item.col != loc1.col))) { h = CloseList [item].Size.col; find = CloseList [item].Parent; } } } while (find != loc1) { old = CloseList [find].Parent; directions.InsertRange (0, direction (old, find)); find = old; } return directions; /* //Вышли т.к. не пути /**/ }