-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcherry.cpp
More file actions
45 lines (41 loc) · 1.41 KB
/
cherry.cpp
File metadata and controls
45 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
int recur(vector<vector<int>>& grid, int x1, int y1, int x2, int n, vector<vector<vector<int>>>&dp, int count)
{
//Base conditions
if(x1>n-1 || y1>n-1 || x2>n-1)return INT_MIN;
if(dp[x1][y1][x2]!=-1)return dp[x1][y1][x2];
if(grid[x1][y1]==-1||grid[x2][x1+y1-x2]==-1)return INT_MIN;
if(x1==n-1 && y1==n-1 && x2==n-1){
if (grid[x1][y1]==1)count++;
//else if(grid[x1][y1]==-1)count = INT_MIN;
dp[x1][y1][x2] = count;
return count;
}
/*
dp[x1][y1][x2] has answer to, starting from here i.e. (x1,y1)&(x2,y2) what is the maximum number of cherries you can collecta
*/
//make answer
dp[x1][y1][x2] = max(recur(grid,x1+1,y1,x2+1,n,dp,count),
max(recur(grid,x1,y1+1,x2+1,n,dp,count),
max(recur(grid,x1+1,y1,x2,n,dp,count),
recur(grid,x1,y1+1,x2,n,dp,count))));
//modify count
count = dp[x1][y1][x2];
if(x1==x2){if(grid[x1][y1]==1)count++;}
else{
if(grid[x1][y1]==1)count++;
if(grid[x2][x1+y1-x2]==1)count++;
}
dp[x1][y1][x2] = count;
return dp[x1][y1][x2];
}
class Solution
{
public:
int cherryPickup(vector<vector<int>>& grid)
{
int n = grid.size();
vector<vector<vector<int>>>dp(n,vector<vector<int>>(n,vector<int>(n,-1)));
int a = recur(grid, 0, 0 ,0, n,dp,0);
return a>0?a:0;
}
};