<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:
    int cherryPickup(vector<vector<int>>& grid) {
        int n = grid.size();
        int smol = -1000000000;
        vector<vector<vector<int>>> dp(n+1, vector<vector<int>> (n+1, vector<int> (n+1, smol)));
        dp[0][0][0] = grid[0][0];
        for (int j = 1; j < n; j++) {
            if (grid[0][j] > -1) {
                dp[0][j][0] = dp[0][j - 1][0] + grid[0][j];
            } else {
                break;
            }
        }
        for (int i = 1; i < n; i++) {
            if (grid[i][0] > -1) {
                dp[i][0][i] = dp[i - 1][0][i - 1] + grid[i][0];
            } else {
                break;
            }
        }
        for (int i = 1; i < n; i++)
        {
            if (grid[0][i] > -1 && grid[i][0] > -1)
            {
                dp[0][i][i] = dp[0][i][0] + dp[i][0][i] - grid[0][0];
                dp[i][0][0] = dp[0][i][0] + dp[i][0][i] - grid[0][0];
            }
            else
            {
                break;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    int a = i, b = j, c = k, d = i+j-k;
                    if (d >= n || d < 0 || grid[a][b] == -1 || grid[c][d] == -1)
                    {
                        continue;
                    }
                    if (a == c && b == d)
                    {
                        if (i - 1 >= 0 && k - 1 >= 0 && dp[i-1][j][k-1] != smol)
                        {
                            dp[i][j][k] = max(dp[i][j][k], grid[i][j] + dp[i-1][j][k-1]);
                        }
                        if (j-1>=0 && dp[i][j-1][k] != smol)
                        {
                            dp[i][j][k] = max(dp[i][j][k], grid[i][j] + dp[i][j-1][k]);
                        }
                        if (j-1>=0 && k-1>=0 && dp[i][j-1][k-1] != smol)
                        {
                            dp[i][j][k] = max(dp[i][j][k], grid[i][j] + dp[i][j-1][k-1]);
                        }
                        if (i-1>=0 && dp[i-1][j][k] != smol)
                        {
                            dp[i][j][k] = max(dp[i][j][k], grid[i][j] + dp[i-1][j][k]);
                        }
                    }
                    else
                    {
                        if (i-1>=0 && k-1>=0 && dp[i-1][j][k-1] != smol)
                        {
                            dp[i][j][k] = max(dp[i][j][k], grid[i][j] + grid[c][d] + dp[i-1][j][k-1]);
                        }
                        if (j-1>=0 && k-1>=0 && dp[i][j-1][k-1] != smol)
                        {
                            dp[i][j][k] = max(dp[i][j][k], grid[i][j] + grid[c][d] + dp[i][j - 1][k-1]);
                        }
                        if (i-1>=0 && d >= 1 && dp[i-1][j][k] != smol)
                        {
                            dp[i][j][k] = max(dp[i][j][k], grid[i][j] + grid[c][d] + dp[i-1][j][k]);
                        }
                        if (j-1>=0 && d >= 1 && dp[i][j-1][k] != smol)
                        {
                            dp[i][j][k] = max(dp[i][j][k], grid[i][j] + grid[c][d] + dp[i][j - 1][k]);
                        }
                    }
                }
            }
        }
        return (dp[n-1][n-1][n-1] == smol ? 0 : dp[n-1][n-1][n-1]);
    }
};
28971