0

I have a programming problem that I am trying to work through, but I'm stuck as in which direction to begin.

The problem is as follows:

If we have some nxn square board (essentially a two-dimensional array) with each square of the grid having a numerical value in it (negative, zero, or positive). The guidelines for the game are that you may start with your "token" at any position on the board, and you can only move your token right or down (in any order). For each square you enter, you add or subtract that total from your score, and your goal is to accumulate the highest score possible before moving off any square on the right or bottom edge.

This is similar to other dynamic programming problems I've seen in the past (word aligning comes to mind), but I'm struggling with where to start without essentially taking a brute force method of dynamic programming (memoize table for each square on the right and bottom edge, but then you end up with (2n tables of size n^2, and the runtime would be atrocious).

What would you recommend as a starting point for this problem in order to achieve the highest possible score, while still keeping the algorithm as time and space efficient as possible?

Eric
  • 13
  • 5
  • You only need one table of size n^2 - the only thing that you need to memoize is the maximum possible score that can be attained from each square. – BJ Myers Mar 11 '17 at 23:11
  • Possible duplicate of [Java - Maximum sum in path through a 2D array](http://stackoverflow.com/questions/11621337/java-maximum-sum-in-path-through-a-2d-array) – Paul Hankin Mar 12 '17 at 00:52

1 Answers1

2

If I understand this correctly, you can just make a nxn table where the entries are

table[i][j] = board[i][j] + max(table[i][j-1], table[i-1][j])

with

table[-1][j] = INT_MIN for all j

and

table[i][-1] = INT_MIN for all i

(table is the table for the maximum scores and board the actual board that you are given.)

Kisar
  • 343
  • 1
  • 11
  • Thank you, I'm not sure how I missed doing that, guess it didn't make sense until I saw it written down. – Eric Mar 11 '17 at 23:24