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 8441513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:34:47+00:00 2026-06-10T08:34:47+00:00

Question: There is an m x n grid ( 0 <= m, n <=

  • 0

Question: There is an m x n grid ( 0 <= m, n <= 500). Each cell in the grid contains k coins (k could be negative or 0 too). You start from 0, 0 and end at m-1, n-1, and you can move either 1 step down or 1 step right, collecting as many coins as you can. If k < 0, then that particular cell has a robber and you can’t move into that cell. If you move into any of the 8 neighboring cells, you will be robbed of k coins. How many coins will you have when you reach m-1, n-1 ?

For example in the grid:

0,23,20,-32
13,14,44,-44
23,19,41,9
46,27,20,0

ans = 129 (by following the path: 0-13-23-46-27-20-0)

Time limit: 5 sec

I don’t think this program can be solved using dynamic programming. And I haven’t studied graph theory (in case it could be used to solve this problem). The straightforward recursive approach is the only thing I can think of, which is too inefficient under the given constraints.

So what would be a good approach to solve it? Don’t just post code, tell me how to begin. If its related to graph theory, then indicating which theorem is involved would be very useful.

  • 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-10T08:34:48+00:00Added an answer on June 10, 2026 at 8:34 am

    I don’t think this program can be solved using dynamic programming.

    Why not? This is a prime candidate for the dynamic programming approach.

    The straightforward recursive approach is the only thing I can think of, which is too inefficient under the given constraints.

    Can you build a recursive solution that solves, say, a 5×5 grid? Perfect! Start with that, and then memoize it by adding an MxN array of best results for cells which you have already solved. Start that array with all large negative values, and then update it when you find a solution that is better. than what’s there already. Once you ‘ve finished with the cell, put the solution into the MxN array: the next time you come there recursively, check the array for a number, and if a value is there, return it without continuing with the recursion.

    The memoized solution itself is rather straightforward. The preprocessing step of the algorithm (subtracting negative numbers from neighboring cells) takes more code.

    int solve(int r, int c) {
        if(memo[r][c] != MIN) {
            return memo[r][c];
        }
        int res = grid[r][c];
        int a = 0, b = 0;
        if (r+1 != R) {
            a = solve(r+1, c);
        }
        if (c+1 != C) {
            b = solve(r, c+1);
        }
        res = max(res+a, res+b);
        return memo[r][c] = res;
    }
    

    Here is the solution on ideone, it returns 129 as expected.

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

Sidebar

Related Questions

i have one question about jquery ajax().. I created a table grid and there
I have One question In My Grid get info from 6 sql server table.
Question: Are there any Mvc framework solutions that I can leverage to create a
This question is about handling arrays of information, there's are many ways I could
I know there have been many questions on grid and pack in the past
There have been some similar questions asked regarding Grid views, but none have been
Newbie question: There are three types of Asp.Net controls : HTML server controls, Web
Here is my question - There are two classes Order and PaymentType . An
I have a job interview tomorrow and I'm trying to answer this question: There
Question: Is there a way to load a lein plugin, say codex or lein-beanstalk,

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.