Q2 (60 points).
You are given an integer N and an N x N grid cherries, where $0 \le cherries[i][j] \le 100$ represents the number of cherries in cell $(i,j)$. Two robots both start at the top-left corner $(0,0)$ and must each reach the bottom-right corner $(N-1,N-1)$. At each step, each robot can move either to the right or downward, but only once. Whenever a robot enters a cell it collects all cherries there, and that cell's cherries become zero. If both robots ever visit the same cell, its cherries are still collected only once. Design and analyze solutions for the following:
a. (30 points) Implement a dynamic-programming (DP) algorithm in Python:
max_cherries(cherries: List[List[int]]) $\to$ int that returns the maximum total cherries the two robots can collect (even if they can't get them all). Recall that if they both step on the same cell you only count its cherries once.
Hints:
* Model both robots' positions by steps: at step k each is at some $(i_1, j_1)$ and $(i_2, j_2)$ with $i_1+j_1 = i_2+j_2 = k$.
* Use a 3D DP table $dp[k][i_1][i_2]$ to store the maximum cherries collected so far.
Example:
Given the following grid $\begin{bmatrix} 1 & 2 & 3 \\ 4 & 1 & 6 \\ 7 & 8 & 9 \end{bmatrix}$ where robots can move either right or down at each step:
If the first robot follows the path $[(0,0), (0,1), (0,2), (1,2), (2,2)]$ and the second robot follows the path $[(0,0), (1,0), (2,0), (2,1), (2,2)]$, then:
* The first robot collects a total of 21 cherries $(1+2+3+6+9)$,
* The second robot collects 19 cherries $(4+7+8)$.
Since both robots start at the same point and end at the same destination without overlapping on cells, the maximum total number of cherries collected is $40 (21+19)$.
b. (10 points) Implement the Python function can_collect_all(cherries: List[List[int]]) $\to$ bool that returns True if it's possible for the two robots, moving simultaneously, to collect every last cherry on the grid between them (i.e. total collected = sum of all cherries $[i][j]$), and False otherwise.
c. (10 points) Derive the time complexity and memory complexity. Discuss why brute-forcing all pairs of paths is infeasible.
d. (10 points) Provide an example grid that would return True when passed to the can_collect_all(cherries: List[List[int]]) function.