java - Prevent Depth First Search Algorithm from getting stuck in an infinite loop ,8 Puzzle -


so code works basic 8 puzzle problems, when test harder puzzle configurations runs infinite loop. can please edit code prevent happening. note have included code prevents loops or cycles. tried including the iterative depth first search technique, did not work. can please review code.

    /** implementation depth first search algorithm */ static boolean depthfirstsearch(string start, string out ){          linkedlist<string> open = new linkedlist<string>();     open.add(start);      set<string> visitedstates = new hashset<string>();      // prevent cyle or loop      linkedlist<string> closed = new linkedlist<string>();      boolean isgoalstate= false;      while((!open.isempty()) && (isgoalstate != true) ){          string x = open.removefirst();          system.out.println(printpuzzle(x)+"\n\n");         jtr.append(printpuzzle(x) +"\n\n");          if(x.equals(out)){               // if x goal             isgoalstate = true;             break;         }         else{                                                                 // generate children of x              linkedlist<string> children = getchildren(x);              closed.add(x);               // put x on closed             open.remove(x);             // since x in closed, take out open              //eliminate children of x if on open or closed ?             for(int i=0; i< children.size(); i++){                 if(open.contains(children.get(i))){                     children.remove(children.get(i));                 }                 else if(closed.contains(children.get(i))){                     children.remove(children.get(i));                 }             }               // put remaining children on left end of open                for(int i= children.size()-1 ; i>=0 ; i--){                 if ( !visitedstates.contains(children.get(i))) {   // check if state visited                        open.addfirst(children.get(i));              // add last child first, , on                         visitedstates.add(children.get(i));                 }             }          }     }          return true;     } 

i suggest putting positions considering https://docs.oracle.com/javase/7/docs/api/java/util/priorityqueue.html priority based on how close being solved.

so take closest position off of queue, , add in of 1 move options there haven't yet been processed. repeat. you'll spent of time exploring possibilities close solved instead of moving randomly forever.

now question "how close solving it". 1 approach take sum of of taxicab distances between squares , need be. better heuristic may give more weight getting squares away corner in place first. if right, changing heuristic should easy.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -