<p>You are given an <code>n x n</code> <code>grid</code> representing a field of cherries, each cell is one of three possible integers.</p>

<ul>
	<li><code>0</code> means the cell is empty, so you can pass through,</li>
	<li><code>1</code> means the cell contains a cherry that you can pick up and pass through, or</li>
	<li><code>-1</code> means the cell contains a thorn that blocks your way.</li>
</ul>

<p>Return <em>the maximum number of cherries you can collect by following the rules below</em>:</p>

<ul>
	<li>Starting at the position <code>(0, 0)</code> and reaching <code>(n - 1, n - 1)</code> by moving right or down through valid path cells (cells with value <code>0</code> or <code>1</code>).</li>
	<li>After reaching <code>(n - 1, n - 1)</code>, returning to <code>(0, 0)</code> by moving left or up through valid path cells.</li>
	<li>When passing through a path cell containing a cherry, you pick it up, and the cell becomes an empty cell <code>0</code>.</li>
	<li>If there is no valid path between <code>(0, 0)</code> and <code>(n - 1, n - 1)</code>, then no cherries can be collected.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/grid.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,-1],[1,0,-1],[1,1,1]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The player started at (0, 0) and went down, down, right right to reach (2, 2).
4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]].
Then, the player went left, up, up, left to return home, picking up one more cherry.
The total number of cherries picked up is 5, and this is the maximum possible.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,-1],[1,-1,1],[-1,1,1]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 50</code></li>
	<li><code>grid[i][j]</code> is <code>-1</code>, <code>0</code>, or <code>1</code>.</li>
	<li><code>grid[0][0] != -1</code></li>
	<li><code>grid[n - 1][n - 1] != -1</code></li>
</ul>

class Solution {
public:
    // function to solve the problem
    int solve(vector<vector<int>>& grid, int r1, int c1, int r2, int c2, int n, int m, vector<vector<vector<vector<int>>>>& dp) {
        // check if both current positions are within the grid and not on a thorn
        if (r1 >= 0 && r2 >= 0 && c1 >= 0 && c2 >= 0 && r1 < n && r2 < n && c1 < m && c2 < m && grid[r1][c1] != -1 && grid[r2][c2] != -1) {
            // check if the result has already been calculated and stored in the dp array
            if (dp[r1][c1][r2][c2] != -1)
                return dp[r1][c1][r2][c2];
                
            // base case: if either of the current positions is at the destination cell, return the value of that cell
            if (r1 == n - 1 && c1 == m - 1)
                return grid[r1][c1];
            if (r2 == n - 1 && c2 == m - 1)
                return grid[r2][c2]; 
            // not needed as r1+c1 = r2+c2 
            // so if r1 == n - 1 && c1 == m - 1
            // then for sure  r2 == n - 1 && c2 == m - 1
            
            int cherry = 0;
            
            // if the two current positions are the same, add the value of that cell to the cherry count
            // otherwise, add the values of both cells to the cherry count
            if (r1 == r2 && c1 == c2)
                cherry += grid[r1][c1];
            else
                cherry += (grid[r1][c1] + grid[r2][c2]);
            
            // calculate the maximum number of cherries by moving both positions in each of the four possible directions
            // and store the result in the dp array
            int op1 = solve(grid, r1, c1 + 1, r2, c2 + 1, n, m, dp);
            int op2 = solve(grid, r1 + 1, c1, r2 + 1, c2, n, m, dp);
            int op3 = solve(grid, r1 + 1, c1, r2, c2 + 1, n, m, dp);
            int op4 = solve(grid, r1, c1 + 1, r2 + 1, c2, n, m, dp);
            return dp[r1][c1][r2][c2] = (cherry += max({op1, op2, op3, op4}));
        }
        // return minimum integer value if the current positions are not valid
        return INT_MIN;
    }

    int cherryPickup(vector<vector<int>>& grid) {
        // initialize dp array with minimum integer value
        vector<vector<vector<vector<int>>>> 
        dp(grid.size(), vector<vector<vector<int>>>(grid.size(), vector<vector<int>>
        (grid.size(), vector<int>(grid.size(), -1))));
        // solve the problem and store the result
        int ans = solve(grid, 0, 0, 0, 0, grid.size(), grid[0].size(), dp);

        if(ans < 0)
            return 0;
            
        return ans;
    }
};
310590