示例#1
0
    public void breadthFirstSearch()
    {
        SceneManager.LoadScene("Maze");
        Node current;

        //for each u E V - {s}
        foreach (Node n in Nodes)
        {
            //u.d = infinite
            n.setDistance(int.MaxValue);
        }
        //s.d = 0
        maze.getEnter().setDistance(0);
        //Q = empty set
        ListQueue queue = new ListQueue();

        //EnQueue(Q,s)
        queue.enqueue(maze.getEnter());
        //While Q != empty set
        while (!queue.isEmpty())
        {
            //u = Dequeue(Q)
            current = (Node)queue.dequeue();
            Debug.Log("(" + current.getX() + "," + current.getY() + ")");
            current.setExplored(true);
            current.setKnown(false);
            if (current == maze.getExit().getPrevious())
            {
                Debug.Log("Breadth First Search Complete");
                break;
            }

            //foreach v E G.Adj[u]
            foreach (Node n in current.getPaths())
            {
                //if v.d == infinite
                if (n.getDistance() == int.MaxValue)
                {
                    //v.d = u.d + 1
                    n.setDistance(current.getDistance() + 1);
                    //EnQueue(Q,v)
                    n.setKnown(true);
                    queue.enqueue(n);
                }
            }
        }
    }
示例#2
0
    public void depthFirstSearch()
    {
        Node current = new Node();

        SceneManager.LoadScene("Maze");
        //for each u E G.V
        foreach (Node n in Nodes)
        {
            //u.color = white
            n.setColor("WHITE");
        }
        //time = 0
        time = 0;
        //for each u E G.V
        ListQueue queue = new ListQueue();

        //EnQueue(Q,s)
        queue.enqueue(maze.getEnter());
        //While Q != empty set
        while (!queue.isEmpty())
        {
            current = (Node)queue.dequeue();
            Debug.Log("(" + current.getX() + "," + current.getY() + ")");
            current.setExplored(true);
            current.setKnown(false);
            if (current == maze.getExit().getPrevious())
            {
                Debug.Log("depth First Search Complete");
                break;
            }

            foreach (Node n in current.getPaths())
            {
                //if u.color == white
                if (n.getColor() == "WHITE")
                {
                    //DFS-VISIT(G,u)
                    Debug.Log("(" + n.getX() + "," + n.getY() + ")");
                    depthFirstSearchVisit(n);
                }
            }
        }
    }