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

  • SEARCH
  • Home
  • 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 8646301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:46:20+00:00 2026-06-12T12:46:20+00:00

The problem I have, I managed to solve recursively, but I wish for a

  • 0

The problem I have, I managed to solve recursively, but I wish for a more optimal solution that would use memoization, but I am not skilled enough to know how it should work in this context.

The problem:

There is an 2d array with random positive integers, and you are given a random amount of moves. You start moving from the top left corner, and the only moves you are allowed to make are:
left
right
down

You need to end at the bottom row and collect as much as you can on your way. You cannot revisit a position.

Memoization is about storing values and building the future results on this, but since there are so many paths one can take, how do I use it? Is it even memoization that I should use or have I made a wrong guess 🙂 ?

  • 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-12T12:46:21+00:00Added an answer on June 12, 2026 at 12:46 pm

    This is basically a dynamic programming problem. You don’t really have to consider all the possible paths at each step since the details of the paths don’t affect future decisions. For each cell, you need to know the maximum amount that can be collected if you go a particular direction and taking a particular number of moves. Nothing else affects your decisions.

    def bestAmount(x,y,direction,n_moves):
      # If this position is off the grid, then this isn't a valid state
      if x<0 or x>=width or y>=height:
        return None
      if n_moves==0:
        # We came to the last move.
        if y!=height-1:
          # If we're not at the bottom row then this isn't a valid path.
          return None
        # Otherwise, the final cell value is part of our total.
        return cellValue(x,y)
      if direction=='down':
        # If we came down to get to this position, then the next move
        # can be either  left, right, or down
        left = bestAmount(x-1,y,'left',n_moves-1)
        right = bestAmount(x+1,y,'right',n_moves-1)
        down = bestAmount(x,y+1,'down',n_moves-1)
        return max(left,right,down)+cellValue(x,y)
      if direction=='left':
        # If we moved left to get to this position, then
        # we can't go right, since we would be visiting the same position again.
        left = bestAmount(x-1,y,'left',n_moves-1)
        down = bestAmount(x,y+1,'down',n_moves-1)
        return max(left,down)+cellValue(x,y)
      if direction=='right':
        # same logic as for left, but opposite.
        right = bestAmount(x+1,y,'right',n_moves-1)
        down = bestAmount(x,y+1,'down',n_moves-1)
        return max(right,down)+cellValue(x,y)
    
    def solve(n_moves):
      # Start by pretending we entered the starting cell by going down
      return bestAmount(0,0,'down',n_moves)
    

    If bestAmount is memoized, then you get a fairly efficient solution since the number of possibilities is relatively limited.

    Treating it as a dynamic programming problem will get you an even more efficient solution though.

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

Sidebar

Related Questions

I have a problem that I have not managed to solve. I developed a
Problem: I have a table that prints out vertical but I would like it
As a C++ oldtimer I have managed to solve my problem but I can
I managed to found out how to delete field but I have a problem
This is more of an intriguing problem than anything else since I have managed
EDIT: I have managed to solve the problem by using: +lorem ipsum +type:photo +lorem
I have a C-code which I have not managed to run http://dl.getdropbox.com/u/175564/problem-sdl.png The problem
I was struggling with one problem today, but managed to solve it by myself.
So I managed to solve a problem of retrieving information from the bundle, but
I have new problem. My code: .method public static void Main() cil managed {

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.