예제 #1
0
 void solve(bfs_node nd)
 {
     while (q.Count >= 1 && !is_goal_done)
     {
         nd = q.Dequeue();
         get_child(nd);
     }
 }
예제 #2
0
        private bfs_node create_node(string state, bfs_node parent)
        {
            bfs_node nd = new bfs_node();

            nd.state  = state;
            nd.parent = parent;
            visited.add(state); q.Enqueue(nd);
            return(nd);
        }
예제 #3
0
        public bfs(string state)
        {
            solution.Clear();
            goal    = get_goal(state.Length);
            visited = new trie(state.Length);
            bfs_node root = create_node(state, null);

            solve(root);
        }
예제 #4
0
        void path(bfs_node nd)
        {
            bfs_node tmp = nd;

            while (tmp != null)
            {
                solution.Push(tmp.state);
                tmp = tmp.parent;
            }
        }
예제 #5
0
        private void add(bfs_node parent, string c_state)
        {
            bfs_node ch_nd = create_node(c_state, parent);

            is_goal_done = is_goal(c_state) ? true : false;
            if (is_goal_done)
            {
                path(ch_nd);
            }
        }
예제 #6
0
        void get_child(bfs_node nd)
        {
            if (is_goal_done)
            {
                return;
            }
            int idx = get_idx(nd.state), x = (int)Math.Sqrt(nd.state.Length);

            if (idx >= x) // move top
            {
                string c_state = generate_state(nd.state, idx - x);
                if (!visited.is_visited(c_state))
                {
                    add(nd, c_state);
                }
            }
            if (idx % x < x - 1 && !is_goal_done) // move right
            {
                string c_state = generate_state(nd.state, idx + 1);
                if (!visited.is_visited(c_state))
                {
                    add(nd, c_state);
                }
            }
            if (idx < (x - 1) * x && !is_goal_done) // move down
            {
                string c_state = generate_state(nd.state, idx + x);
                if (!visited.is_visited(c_state))
                {
                    add(nd, c_state);
                }
            }
            if (idx % x > 0 && !is_goal_done) // move left
            {
                string c_state = generate_state(nd.state, idx - 1);
                if (!visited.is_visited(c_state))
                {
                    add(nd, c_state);
                }
            }
        }