/*-----------------------------------*/ /* 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()); }
public DLSNode(DLSNode p, string a, int h) { parent = p; action = a; height = h; }
public string GetSolution() { DLSNode sol = this; string ans = string.Empty; while (sol.parent != null) { ans = sol.action + " " + ans; sol = sol.parent; } return(ans); }