Results 1 to 2 of 2
  1. #1
    Ivanniki is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help With class assignment - recursion

    Quote Originally Posted by Ivanniki View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need help with class Month assignment please!!
    By ndsmith20 in forum New To Java
    Replies: 5
    Last Post: 10-17-2012, 10:26 PM
  2. Help needed with class assignment.
    By DigDug in forum New To Java
    Replies: 9
    Last Post: 01-24-2012, 04:52 AM
  3. Need help with Instrument Class assignment
    By Kinney.j in forum New To Java
    Replies: 0
    Last Post: 11-02-2011, 04:42 AM
  4. Recursion assignment problems
    By tfitz666 in forum New To Java
    Replies: 11
    Last Post: 01-24-2010, 09:18 PM

Posting Permissions

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