Ejemplo n.º 1
0
    public ShipPath ConcatenatePath(ShipPath path)
    {
        ShipPath newPath = new ShipPath();

        foreach (Node node in this.path)
        {
            newPath.AppendNode(node);
        }
        foreach (Node node in path.path)
        {
            newPath.AppendNode(node);
        }
        return(newPath);
    }
Ejemplo n.º 2
0
    public List <ShipPath> FindPath(Node from, Node to)
    {
        int             maxNumPaths   = 2;
        int             numPaths      = 0;
        List <ShipPath> allPaths      = new List <ShipPath>();
        List <TreeNode> nodesToExpand = new List <TreeNode>();

        if (from == to)
        {
            ShipPath path = new ShipPath();
            path.AppendNode(to);
            allPaths.Add(path);
            return(allPaths);
        }

        nodesToExpand.Add(new TreeNode(null, from));
        while (nodesToExpand.Count > 0)
        {
            TreeNode treeNodeToExpand = nodesToExpand[0];
            nodesToExpand.RemoveAt(0);

            List <Node> connectedNodes = GetAllConnectedNode(treeNodeToExpand.node);
            foreach (Node connectedNode in connectedNodes)
            {
                // This is per-path way to avoid repeatance. Consider change it to global way.
                if (isNodeAlreadyOnPath(connectedNode, treeNodeToExpand))
                {
                    continue;
                }

                TreeNode child = new TreeNode(treeNodeToExpand, connectedNode);

                if (connectedNode == to)
                {
                    allPaths.Add(GetPathFromTreeNode(child));
                    numPaths++;
                    if (numPaths > maxNumPaths)
                    {
                        return(allPaths);
                    }
                }
                else
                {
                    nodesToExpand.Add(child);
                }
            }
        }

        return(allPaths);
    }