Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:04:16+00:00 2026-06-08T10:04:16+00:00

Basically I have a problem that goes something similar to this: There is a

  • 0

Basically I have a problem that goes something similar to this:

There is a garden of strawberry plants represented by a 2D, square array. Each plant(each element) has a number of strawberries. You start at the top left corner of the array, and you can only move to the right or down. I need to design a recursive method to calculate the paths through the garden and then output which one yields the most strawberries.

I think I have an understanding of really really simple recursion problems, but this problem has gone way over my head. I’m not really sure where to start or where to go as far as creating a recursive method.

Any help related to the code or helping me understand the concept behind this problem is greatly appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:04:20+00:00Added an answer on June 8, 2026 at 10:04 am

    Like dasblinkenlight said, the most efficient way to do this is using a memoization or dynamic programming technique. I tend to prefer dynamic programming, but I’ll use pure recursion here.

    The answer centers around the answer to one fundamental question: “If I’m in the square in row r and column c on my field, how can I evaluate the path from the top left to here such that the number of strawberries is maximized?”

    The key to realize is that there’s only two ways to get in the plot in row r and column c: either I can get there from above, using the plot in row r-1 and column c, or I can get there from the side, using the plot in row r and column c-1. After that, you just need to make sure you know your base cases…which means, fundamentally, my purely recursive version would be something like:

    int[][] field;    
    int max(int r, int c) {
        //Base case
        if (r == 0 && c == 0) {
            return field[r][c];
        }
        //Assuming a positive number of strawberries in each plot, otherwise this needs
        //to be negative infinity
        int maxTop = -1, maxLeft = -1;
        //We can't come from the top if we're in the top row
        if (r != 0) {
            maxTop = field[r-1][c];
        }
        //Similarly, we can't come from the left if we're in the left column
        if (c != 0) {
            maxLeft = field[r][c-1];
        }
        //Take whichever gives you more and return..
        return Math.max(maxTop, maxLeft) + field[r][c];
    }
    

    Call max(r-1, c-1) to get your answer. Notice there’s a lot of inefficiency here; you’ll do much better by using dynamic programming (which I’ll provide below) or memoization (which has already been defined). The thing to remember, though, is that both the DP and memoization techniques are simply more efficient ways that come from the recursive principles used here.

    DP:

    int maxValue(int[][] field) {
        int r = field.length;
        int c = field[0].length;
        int[][] maxValues = new int[r][c];
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                if (i == 0 && j == 0) {
                    maxValues[i][j] = field[i][j];
                } else if (i == 0) {
                    maxValues[i][j] = maxValues[i][j-1] + field[i][j];
                } else if (j == 0) {
                    maxValues[i][j] = maxValues[i-1][j] + field[i][j];
                } else {
                    maxValues[i][j] = Math.max(maxValues[i][j-1], maxValues[i-1][j]) + field[i][j];
                }
            }
        }
        return maxValues[r-1][c-1];
    }
    

    In both cases, if you want to recreate the actual path, just keep a 2D table of booleans that corresponds with “Did I come from above or to the left”? If the most strawberry path comes from above, put true, otherwise put false. That can allow you to retrace the patch after the calculation.

    Notice that this is still recursive in principal: at each step, we’re looking back at our previous results. We just happen to be caching our previous results so we don’t waste a bunch of work, and we’re attacking the subproblems in an intelligent order so that we can always solve them. For more on dynamic programming, see Wikipedia.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty simple problem. Basically I have an array called $list that
I'm having a Hibernate query problem, basically it goes like this ... I have
Basically I have this problem. I have an accordion that toggles one heading open
I have some localization problems in my webpage. There are basically two problems (that
Basically i have a problem with this timer program I am trying to put
I have basically the same problem outlined in this question, however I am using
Well, this is the exact opposite problem that I normally have with Javascript synchronicity
Basically I have a page named dvds.asp with a form that goes to action=process.asp
So I have some perl code that goes something like: use strict; use XML::XPath;
I basically have this problem: right now, we have a system where it gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.