Results 1 to 2 of 2
- 12-10-2012, 02:41 PM #1
Member
- 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
Thanks for the helpJava 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.length-1) if(isSolvable(start+board[start],board)) return true; if(start-board[start]>0) if(isSolvable(start-board[start],board)) return true; } return false; } public static boolean valid(int start) { if(first[start]==false) { first[start]=true; return true; } return false; } }
- 12-10-2012, 04:48 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
Re: Help With class assignment - recursion
That should be:
Arrays start at index value 0 and the highest index value is n-1 where n is the length of the array.Java Code:if(start+board[start]<board.length) if(isSolvable(start+board[start],board)) return true; if(start-board[start]>=0) if(isSolvable(start-board[start],board)) return true; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Need help with class Month assignment please!!
By ndsmith20 in forum New To JavaReplies: 5Last Post: 10-17-2012, 10:26 PM -
Help needed with class assignment.
By DigDug in forum New To JavaReplies: 9Last Post: 01-24-2012, 04:52 AM -
Need help with Instrument Class assignment
By Kinney.j in forum New To JavaReplies: 0Last Post: 11-02-2011, 04:42 AM -
Recursion assignment problems
By tfitz666 in forum New To JavaReplies: 11Last Post: 01-24-2010, 09:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks