<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>

int row[]={1,0,-1,0};
int col[]={0,1,0,-1};
class Solution {
public:
   
    bool path(int i,int j,int n,vector<vector<int>>&v,vector<vector<int>>&vis){
           if(i==n-1 && j==n-1){
             return v[i][j]; 
           }
           if(vis[i][j]){
            return vis[i][j];
           }
           for(int k=0;k<2;k++){
                int r=row[k]+i;
                int c=col[k]+j;
                if(r<n && c<n && v[r][c]!=-1){
                     if(path(r,c,n,v,vis)){
                        return vis[i][j]= true;
                     }
                }
           }
         return vis[i][j]= false;
    }
    int solve(int i,int j,int x,int y,int n,vector<vector<int>>&v,vector<vector<vector<vector<int>>>>&dp){
           if(i==n-1 && j==n-1){
            // cout<<"DD";
            return 0;
           }
           int ans=INT_MIN;
           if(dp[i][j][x][y]!=-1)return dp[i][j][x][y];
           for(int k=0;k<2;k++){
              for(int l=0;l<2;l++){
                 int r1=row[k]+i;
                 int r2=row[l]+x;
                 int c1=col[k]+j;
                 int c2=col[l]+y;
                 if(r1<n && c1<n && r2<n && c2<n){
                     if(r1==r2 && c1==c2 && v[r1][c1]!=-1){
                        ans=max(ans,v[r1][c1]+solve(r1,c1,r2,c2,n,v,dp));
                     }
                     else if(v[r1][c1]!=-1 && v[r2][c2]!=-1){
                        ans=max(ans,v[r1][c1]+v[r2][c2]+solve(r1,c1,r2,c2,n,v,dp));
                     }
                 }
              }
           }
           return dp[i][j][x][y]=ans;
    }
    int cherryPickup(vector<vector<int>>& grid) {
        int n=grid.size();
        if(n==1)return grid[0][0];
        vector<vector<int>>vis(n,vector<int>(n,0));
        vector<vector<vector<vector<int>>>>dp(n,vector<vector<vector<int>>>(n,vector<vector<int>>(n,vector<int>(n,-1))));
        // if(path(0,0,n,grid,vis)){
            int num1=grid[0][0]+solve(0,0,0,0,n,grid,dp);
            // cout<<INT_MIN<<endl;
            return num1<0?0:num1;
        // }
        // return 0;
    }
};
303721