Advertisement
markianovnikita

Vot tak vot

Feb 26th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. Cначала определю классы
  2.  
  3. Point
  4. {
  5. public double X;
  6. public double Y;
  7. }
  8.  
  9. Node
  10. {
  11. public Point Point;
  12. public double PathLength;
  13. public List<Node> connections;
  14. }
  15.  
  16. Теперь алгоритм:
  17.  
  18. foreach(var node in allNodes)
  19. {
  20.     var shape = FindCycleFromNode(node);
  21. }
  22.  
  23. List<Node> FindCycleFromNode(Node targetNode)
  24. {
  25.     List<Node> openList = new ...
  26.     List<Node> closedList = new ...
  27.  
  28.     closedList.Add(targetNode);
  29.  
  30.     Node currentNode = targetNode;
  31.  
  32.     do
  33.     {
  34.         foreach(var connectedNode in currentNode.Connections)
  35.         {
  36.             if(closedList.Contains(connectedNode) continue;
  37.  
  38.             if(openList.Contains(connectedNode)
  39.             {
  40.                 if(connectedNode.PathLength > currentNode.PathLength + Distance(currentNode.Point, connectedNode.Point))
  41.                 {
  42.                     connectedNode.PathLength = currentNode.PathLength + Distance(currentNode.Point, connectedNode.Point);
  43.                 }
  44.                 else if(Distance(connectedNode.Point, targetNode.Point) == connectedNode.PathLength)//Если значение ноды равно длине до целевой точки.
  45.                 {
  46.                     connectedNode.PathLength = currentNode.PathLength + Distance(currentNode.Point, connectedNode.Point);
  47.                 }
  48.             }
  49.             else
  50.             {
  51.                 connectedNode.PathLength = currentNode.PathLength + Distance(currentNode.Point, connectedNode.Point);
  52.                 openList.Add(connectedNode);
  53.             }
  54.         }
  55.  
  56.         closedList.Add(currentNode);
  57.         openList.Remove(currentNode);
  58.  
  59.         var nextNode = FindNodeInOpenListWithMinLength(openList);
  60.         currentNode = nextNode;
  61.  
  62.     }while(openList.Count > 0);
  63.  
  64.     Node pathNode = null;
  65.     foreach(var node in targetNode.Connections)
  66.     {
  67.         if(node.PathLength > Disntance(node.Point, targetNode.Point))
  68.         {
  69.             if(pathNode == null) pathNode = node;
  70.             else if(pathNode.Length > node.PathLength) pathNode = node;
  71.         }
  72.     }
  73.  
  74.     List<Node> path = new ...
  75.     path.Add(pathNode);
  76.     while(pathNode != null)
  77.     {
  78.         var nextPathNode = FindMinNode(pathNode.Connections);
  79.         if(nextPathNode == targetNode)
  80.         {
  81.             path.Add(nextPathNode);
  82.             pathNode = null;
  83.         }
  84.         else
  85.         {
  86.             path.Add(nextPathNode);
  87.             pathNode = nextPathNode;
  88.         }
  89.     }
  90.  
  91.     return path;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement