Results 1 to 2 of 2
 12102012, 03:41 PM #1Member
 Join Date
 Dec 2012
 Posts
 1
 Rep Power
 0
Help With class assignment  recursion
Hey,
got this class assignment to recursively move through an array of numbers
and return true or false whether is possible to reach the zero
you start at leftmost part of the array and the goal is to get to the 0 in the rightmost part
each step is determined by the int on the current array[step]
3 is the first step which means you move to 1 because its 3 columns away,
you can move either right or left (if allowed to reach the goal)
{3, 6, 4, 1, 3, 4, 2, 5, 3, 0}
unsolvable example
{3, 1, 2, 3, 0}
Here, one can bounce between the two 3’s, but cannot reach any other square.
here is my try at the solution but it doesn't work for all the different arrays
Java Code:public class GetToTheZero { public static boolean [] first,second; public static boolean isSolvable(int start, int[] board) { first=new boolean[board.length]; boolean solution=check (start, board); return solution; } public static boolean check (int start, int[] board) { if(board[start]==0) return true; if(valid(start)) { if(start+board[start]<board.length1) if(isSolvable(start+board[start],board)) return true; if(startboard[start]>0) if(isSolvable(startboard[start],board)) return true; } return false; } public static boolean valid(int start) { if(first[start]==false) { first[start]=true; return true; } return false; } }
 12102012, 05:48 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,312
 Blog Entries
 7
 Rep Power
 25
Re: Help With class assignment  recursion
That should be:
Java Code:if(start+board[start]<board.length) if(isSolvable(start+board[start],board)) return true; if(startboard[start]>=0) if(isSolvable(startboard[start],board)) return true; }
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

Need help with class Month assignment please!!
By ndsmith20 in forum New To JavaReplies: 5Last Post: 10172012, 10:26 PM 
Help needed with class assignment.
By DigDug in forum New To JavaReplies: 9Last Post: 01242012, 05:52 AM 
Need help with Instrument Class assignment
By Kinney.j in forum New To JavaReplies: 0Last Post: 11022011, 05:42 AM 
Recursion assignment problems
By tfitz666 in forum New To JavaReplies: 11Last Post: 01242010, 10:18 PM
Bookmarks