problem link–>https://www.geeksforgeeks.org/gold-mine-problem/
Please help me in my code this is gold mining problem where i am doing wrong?
Hi Kailash
You have not initalized the base case values in the dp[][] matrix. Currently your dp[][] returns 0 for last column or last row but that is not the correct answer. Initialize the base case conditions
in line 39 i am giving vlaue to dp this should be correct
please tell me where i should put base case for dp matrix
where you have created your dp matrix. Set the deafult base values from the a[][] matrix for those whose values are already known.
You have still not implemented the base cases.
for
(
int
col=n-1; col>=0; col--)
{
for
(
int
row=0; row<m; row++)
{
// Gold collected on going to the cell on the right(->)
int
right = (col==n-1)? 0: goldTable[row][col+1];
// Gold collected on going to the cell to right up (/)
int
right_up = (row==0 || col==n-1)? 0:
goldTable[row-1][col+1];
// Gold collected on going to the cell to right down (\)
int
right_down = (row==m-1 || col==n-1)? 0:
goldTable[row+1][col+1];
// Max gold collected from taking either of the
// above 3 paths
goldTable[row][col] = gold[row][col] +
max(right, max(right_up, right_down));
}
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.