Exemplo n.º 1
0
        /*-----------------------------------*/

        /* Search algorithms
         * ------------------------------------------------------------------------------------------*/
        //DLS uninformed search - recursive implementaion
        public string DLS(int l)
        {
            DLSNode sol = null;
            var     set = new HashSet <string>();

            void FindSolution(DLSNode n)
            {
                if (set.Add(GetHash()))
                {     //If the state is new
                    if (CanReachGoal())
                    { //Found solution
                        sol = new DLSNode(n, "XR" + (BOARD_SIZE - cars['X'].posX), n.height + 1);
                    }
                    else if (n.height < (l - 1) && sol == null)
                    {   //Add possible moves to the stack
                        var moves = PossibleMoves();
                        for (int i = 0; i < moves.Length && sol == null; i++)
                        {
                            Move(moves[i]);
                            FindSolution(new DLSNode(n, moves[i], n.height + 1));
                            Move(OppositeMove(moves[i]));
                        }
                    }
                }
            }

            FindSolution(new DLSNode(null, null, 0));
            return(sol.GetSolution());
        }
Exemplo n.º 2
0
 public DLSNode(DLSNode p,
                string a,
                int h)
 {
     parent = p;
     action = a;
     height = h;
 }
Exemplo n.º 3
0
            public string GetSolution()
            {
                DLSNode sol = this;
                string  ans = string.Empty;

                while (sol.parent != null)
                {
                    ans = sol.action + " " + ans;
                    sol = sol.parent;
                }
                return(ans);
            }