示例#1
0
        /// <summary>
        /// implimantion of the BFS algorithm
        /// </summary>
        /// <remarks>1. insert to queue the start state and insert start state to m_visited
        /// 2. while queue is not empty
        /// 2.1 dequeue state and mark as v
        /// 2.1 if v is the goal
        /// 2.1.1 get solution
        /// 2.2 else
        /// 2.2.1 if v is not in m_visited
        /// 2.2.1.1 insert v to m_visited
        /// 2.2.1.2 insert to queue all neigbors of v</remarks>
        /// <param name="search"></param>
        /// <returns></returns>
        public Solution Solve(ISearchable search)
        {
            StartTiming();
            m_visited.Clear();
            m_numofnodes = 1;
            queue.Enqueue(search.getInitialState());
            m_visited.Add(search.getInitialState().ToString(), search.getInitialState());
            Solution sol  = new Solution();
            Astate   goal = search.getGoalState();

            while (queue.Count != 0)
            {
                Astate vertex = queue.Dequeue();
                if (vertex.Equals(goal))
                {
                    sol = backtrack(vertex);
                    break;
                }
                List <Astate> list = search.getAllPossibleStates(vertex);
                foreach (Astate state in list)
                {
                    if (!m_visited.ContainsKey((state as MazeState).currentp.ToString()))
                    {
                        m_visited.Add((state as MazeState).currentp.ToString(), state);
                        queue.Enqueue(state);
                        m_numofnodes++;
                        state.cameFrom = vertex;
                    }
                }
            }
            StopTiming();
            return(sol);
        }
示例#2
0
        /// <summary>
        /// implimantion of the DFS algorithm
        /// </summary>
        /// <remarks>1. insert the start state to the stack.
        /// 2. While stack not empty
        /// 2.1 pop from stack and insert to v
        /// 2.2 if v is the goal
        /// 2.2.1 get solution
        /// 2.3 if v is not in the visited list:
        /// 2.3.1 insert v to m_visited
        /// 2.3.2 for all neighbors of v, push neighbor to stack</remarks>
        /// <param name="search"></param>
        /// <returns></returns>
        public Solution Solve(ISearchable search)
        {
            StartTiming();
            m_visited.Clear();
            m_numofnodes = 1;
            stack.Push(search.getInitialState());
            Solution sol  = new Solution();
            Astate   goal = search.getGoalState();

            while (stack.Count != 0)
            {
                Astate vertex = stack.Pop();
                if (vertex.Equals(goal))
                {
                    sol = backtrack(vertex);
                    break;
                }
                if (!m_visited.ContainsKey((vertex as MazeState).currentp.ToString()))
                {
                    m_visited.Add((vertex as MazeState).currentp.ToString(), vertex);
                }
                List <Astate> lolist = search.getAllPossibleStates(vertex);
                foreach (Astate a in lolist)
                {
                    if (!m_visited.ContainsKey((a as MazeState).currentp.ToString()))
                    {
                        a.cameFrom = vertex;
                        stack.Push(a);
                        m_numofnodes++;
                    }
                }
            }
            StopTiming();
            return(sol);
        }