 I have not included the condition to not go for positions having zero but shold only change the answer at max and not cause segmentation fault.

``````using namespace std;
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
// #include <bits/stdc++.h>

class Solution
{
public:
int travelInmine(int r, int c, vector<vector<int>> &grid)
{
if(c >=grid.size() || c < 0){
return 0;
}
if(r >=grid.size() || r < 0){
return 0;
}
int up = travelInmine(r-1,c,grid);
int down = travelInmine(r+1,c,grid);
int left = travelInmine(r,c-1,grid);
int right = travelInmine(r,c+1,grid);
return grid[r][c] + max({up,down,right,left});
}
int getMaximumGold(vector<vector<int>> &grid)
{
int mxm = 0;
for (int i = 0; i < grid.size(); i++)
{
if(grid[i] != 0){
mxm = max(mxm, travelInmine(i, 0, grid));
}
}
return mxm;
}
};

int main()
{
// code here
vector<vector<int>> mat{{1, 0, 7},
{2, 0, 6},
{3, 4, 5},
{0, 3, 0},
{9, 0, 20}};
Solution s;
cout << s.getMaximumGold(mat);
return 0;
}

``````
• The code is a function that takes in two vectors of integers and returns the maximum value found.
• The code starts by declaring an int variable called and(or anything you like), which is initialized to 0.
• Then it iterates over each element in the grid vector, starting with i and ending with j.
• The for loop then iterates through each element in the grid[i][j] vector, where i + x >= 0 && j + y >= 0 && i + x < grid.size() && j + y < grid.size() && grid[i+x][j+y] != 0) { int max = dfs(grid, i + x, j + y); ans = max; }
• The code will return the maximum value of gold in a grid.
• The code above takes in a vector of vectors and iterates through each row to find the maximum value for each row.

Hey @radwanromy Thanks for taking time to help, i got your solution nicely written. But i wanted find the error in my given code.