Of course, there are no adjacent cells
above the cells in the first row, and there are no adjacent cells to the left of the
cells in the first column. For those cells, we assume that F (i − 1,j) and F (i, j − 1)
are equal to 0 for their nonexistent neighbors. Therefore, the largest number of
coins the robot can bring to cell (i, j ) is the maximum of these two numbers plus
one possible coin at cell (i, j ) itself. In other words, we have the following formula
for F (i, j ):
F (i, j ) = max{F (i − 1, j ), F (i, j − 1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m
F (0, j) = 0 for 1 ≤ j ≤ m and F (i, 0) = 0 for 1 ≤ i ≤ n,
designing the knapsack problem using dp:
f(i, j) = f(i - 1,j) j - w < 0
f(i, j) = max(f(i - 1,j), v + f(i - 1, j - w) j - w >= 0
f(0, j) = 0 j>=0
f(i, 0) = 0 i>=0