<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) {
        // an observation: max number of cherries that you can pick up by starting from (0, 0)
        // to (n - 1, n - 1) and then going from (n - 1, n - 1) to (0, 0) 
        // is no different from just going from (0, 0) to (n - 1, n - 1) two times
        // 
        // can do dp:
        // dp[t][x1][x2]: the max number of cherries you can pick up after t moves
        // where the first time you are at x1 and the second time you are at x2
        // you can thus calculate y1 = t - x1 and y2 = t - x2
        //
        // to enforce the fact that you can't pick up the same cherry twice, if x1 == x2
        // and you are on a cherry, then only add 1 to the dp value instead of 2
        // otherwise if x1 != x2 and both are on cherries, add 2 to the dp value
        int n = grid.size();
        int max_time = 2 * n - 1;
        vector<vector<vector<int>>> dp(max_time, vector<vector<int>>(n, vector<int>(n, -1)));
        // base case: dp[0][0][0] = grid[0][0] // if there's a cherry at (0, 0) then it's picked up automatically.
        dp[0][0][0] = grid[0][0];
        vector<vector<int>> dir{
            vector<int>{1, 0, 1, 0},
            vector<int>{1, 0, 0, 1},
            vector<int>{0, 1, 1, 0},
            vector<int>{0, 1, 0, 1}
        };
        for (int t = 0; t < max_time - 1; t++) {
            for (int x1 = 0; x1 < n; x1++) {
                for (int x2 = 0; x2 < n; x2++) {
                    if (dp[t][x1][x2] == -1) continue;
                    int y1 = t - x1;
                    int y2 = t - x2;
                    if (y1 < 0 || y2 < 0 || y1 >= n || y2 >= n) continue;
                    vector<int> state{x1, y1, x2, y2};
                    //cout << state.size() << endl;
                    for (const auto &d: dir) {
                        vector<int> cur(state);
                        //cout << cur.size() << endl;
                        bool valid = true;
                        for (int i = 0; i < 4; i++) {
                            cur[i] += d[i];
                            if (cur[i] >= n) valid = false;
                        }
                        if (valid) {
                            if (grid[cur[0]][cur[1]] == -1) valid = false;
                            if (grid[cur[2]][cur[3]] == -1) valid = false;
                        }
                        if (valid) {
                            int gained_cherries = 0;
                            if (grid[cur[0]][cur[1]]) gained_cherries++;
                            if (grid[cur[2]][cur[3]] && !(cur[0] == cur[2] && cur[1] == cur[3])) gained_cherries++;
                            dp[t + 1][cur[0]][cur[2]] = max(dp[t + 1][cur[0]][cur[2]], dp[t][x1][x2] + gained_cherries);
                        }
                    }
                }
            }
        }
        return dp[max_time - 1][n - 1][n - 1] == -1 ? 0 : dp[max_time - 1][n - 1][n - 1];
    }
};
97659