<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 dp[55][55][55][55]; 
    int n, m;
    vector<vector<int>> g; 

    int rec(int i, int j, int a, int b) {
        if(g[i][j] == -1 || g[a][b] == -1) return -1e9; 

        if(i==n-1 && j==m-1 && a==n-1 && b==m-1) {
            return g[i][j]; 
        }

        if(dp[i][j][a][b] != -1) 
        {
            return dp[i][j][a][b]; 
        }
        int temp = -1e9;

        vector<vector<int>> mv = {{1,0,1,0}, {1,0,0,1}, {0,1,1,0}, {0,1,0,1}}; 
        for(auto it: mv) {
            int ii = i + it[0], jj = j + it[1]; 
            int aa = a + it[2], bb = b + it[3]; 

            if(jj>=0 && jj<m && bb>=0 && bb<m && ii<n && aa<n && g[ii][jj] != -1 && g[aa][bb] != -1) {
                temp = max(temp, rec(ii, jj, aa, bb)); 
            }
        }

        int ans = g[i][j] + g[a][b]; 
        if(i==a && j==b) {
            ans -= g[i][j]; 
        }
        if(temp != -1e9) {
            temp += ans; 
        }

        // cout << i << " " << j << " " << a << " " << b << temp << endl; 

        return dp[i][j][a][b] = temp; 
    }

    int cherryPickup(vector<vector<int>>& grid) {
        g = grid; 
        n = grid.size(); 
        m = grid[0].size(); 
        memset(dp, -1, sizeof(dp)); 

        // for(int i=0; i<n; i++) {
        //     for(int j=0; j<m; j++) cout << dp[i][j]; 
        //     cout << endl; 
        // }

        int ans = rec(0, 0, 0, 0); 
        if(ans == -1e9) return 0; 
        return ans; 
    }
};
200690