The Artima Developer Community
Sponsored Link

Java Answers Forum
Printing Correct Portions of a Recursion

2 replies on 1 page. Most recent reply: Apr 3, 2007 11:44 AM by Pete Burkindine

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Pete Burkindine

Posts: 4
Nickname: repeat2341
Registered: Apr, 2007

Printing Correct Portions of a Recursion Posted: Apr 3, 2007 11:43 AM
Reply to this message Reply
Advertisement
Hi!

This is my first post; I'm a first you CS major. I'm trying to find a way to write a "verbose" add-on method for an assignment. The assignment is to play the game JumpIt, where the player either moves one or two spaces to the right through a board/array of integers to reach the final square, shooting for the smallest sum. The problem is solved recursively; here's the simple method (in Java):

private static int findRoute(int start, int[] board) {
if ((boardSize - start) == 0 ) {
// the last element has been reached
return board[start];
}
else {
return board[start] + Math.min(findRoute(start + 1, board), findRoute(start + 2, board));
} // end else
} // end findRoute


What I want to do is print out a string of just those steps that lead to the best solution. So far, my attempts to build such a string have all had many hundreds of entries, in wild order, resulting in nothing of interest. I'm stumped.

Does anyone know how I could do this? It's not part of the assignment; I just want to know. Thanks in advance!

Pete


Pete Burkindine

Posts: 4
Nickname: repeat2341
Registered: Apr, 2007

Re: Printing Correct Portions of a Recursion Posted: Apr 3, 2007 11:44 AM
Reply to this message Reply
PS - since a board's size is unchanging, the boardSize variable is set in the wrapper method for findRoute as a class variable in my version.

Pete Burkindine

Posts: 4
Nickname: repeat2341
Registered: Apr, 2007

Re: Printing Correct Portions of a Recursion Posted: Apr 3, 2007 11:44 AM
Reply to this message Reply
Also, I'm a first YEAR, not a first "you", CS major. Huh.

Flat View: This topic has 2 replies on 1 page
Topic: Printing Correct Portions of a Recursion Previous Topic   Next Topic Topic: Odwrócony

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use