i am not able to find the correct ans
I need a help in gold mine problem plz help
Made some changes now i think it should work fine.
#include<bits/stdc++.h>
using namespace std;
int dp[4][4];
int getMaxGold(int ar[4][4],int i,int j){
if(j>3)return 0;
if(i<0||i>3)return 0;
if(dp[i][j]!=-1) return dp[i][j];
int right=0,up=0,down=0;
right=getMaxGold(ar,i,j+1);
up=getMaxGold(ar,i-1,j+1);
down=getMaxGold(ar,i+1,j+1);
dp[i][j]=max(up,max(right,down))+ar[i][j];
return dp[i][j];
}
int main(){
int gold[4][4]=
{{1, 3, 1, 5},
{2, 2, 4, 1},
{5, 0, 2, 3},
{0, 6, 1, 2}
};
int m = 4, n = 4;
memset(dp,-1,sizeof(dp));
// for(int i=0;i<4;i++)
cout << getMaxGold(gold, 3, 0)<<endl;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
u should not return 0 when i==3 or j==3 u should return 0 when i>3 or j>3.