Skip to content

Instantly share code, notes, and snippets.

@Kumawatlalit912
Created September 10, 2022 15:54
Show Gist options
  • Save Kumawatlalit912/37461a9f65b6efef8558ff955cd75caf to your computer and use it in GitHub Desktop.
Save Kumawatlalit912/37461a9f65b6efef8558ff955cd75caf to your computer and use it in GitHub Desktop.
3d dp concept chocolate pickup
#include<bits/stdc++.h>
const int mod=-1e9+7;
int dp[50][50][50];
int solve(int i,int j1,int j2,vector<vector<int>>&grid,int r,int c){
if( j1<0 || j2<0 || j1>c-1 || j2>c-1 ) return mod;
if(i==r-1){
if(j1==j2) return grid[i][j1];
else{
return grid[i][j1]+grid[i][j2];
}
}
if(dp[i][j1][j2]!=-1) return dp[i][j1][j2];
int maxi=0;
// for each alice and bob there are 3 direction so in total ther are 9 direction
for(int dj1=-1;dj1<=1;dj1++){
for(int dj2=-1;dj2<=1;dj2++){
if(j1==j2){
maxi=max(maxi,grid[i][j1]+solve(i+1,j1+dj1,j2+dj2,grid,r,c));
}
else{
maxi=max(maxi,grid[i][j1]+grid[i][j2]+solve(i+1,j1+dj1,j2+dj2,grid,r,c));
}
}
}
return dp[i][j1][j2]=maxi;
}
int maximumChocolates(int r, int c, vector<vector<int>> &grid) {
// Write your code here.
memset(dp,-1,sizeof dp);
return solve(0,0,c-1,grid,r,c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment