# Thread: Help With class assignment - recursion

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
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.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;
}
}```
Thanks for the help  Reply With Quote

2. ## Re: Help With class assignment - recursion Originally Posted by Ivanniki 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.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;
}
}```
Thanks for the help
That should be:

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;
}```
Arrays start at index value 0 and the highest index value is n-1 where n is the length of the array.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•