Stuck on Java Recursion Puzzle -


i new java , have decided learn recursion. there puzzle must solve using recursive method. puzzle list on integers start @ first element , goal reach last element, zero. have move right or left in list , number of times can determined element on. cannot move outside list either, , cannot use sublist. method suppose see if given puzzle solvable. current code has stackoverflow , have no idea how fix it

there few problems algorithm.

the last thing method 'issolveable' returns forward or backwards solveability. instead of returning, try forward solveability first, if fails, try backwards solveability.

the 'beenon' list isn't passed each issolveable() method invocation empty each time method called. can create 1 on first call , pass along each time call method.

also arn't checking if out of bounds quite right. list.size() 1 greater actual index of last position in list, since lists start @ 0, not 1. last index board.size() - 1

public static boolean issolvable(int index, list<integer> board, list<integer> visitedindices) {     int lastindex = board.size() - 1;     if (index == lastindex) {         // last index. solved         return true;     }      if (index > lastindex || index < 0 || visitedindices.contains(index)) {         // outside list or vistied, failed solve         return false;     }      visitedindices.add(index);     int nextindexforward = index + board.get(index);     int nextindexbackward = index - board.get(index);      return issolvable(nextindexforward, board, visitedindices)            || issolvable(nextindexbackward, board, visitedindices); } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -