解题思路:
(1)递归求解
class Solution {
public:
void helper(vector<int> vis,vector<vector<int>>& grid,vector<int> &v,vector<vector<int>> &d,
int x,int y,int m,int n,int temp) {
temp += grid[x][y];
vis[x*n+y] = 1;
for(int i=0;i<d.size();i++) {
if(0<=x+d[i][0] && x+d[i][0]<m && 0<=y+d[i][1] && y+d[i][1]<n &&
vis[(x+d[i][0])*n+y+d[i][1]]==0 && grid[x+d[i][0]][y+d[i][1]]!=0) {
helper(vis,grid,v,d,x+d[i][0],y+d[i][1],m,n,temp);
} else v.push_back(temp);
}
}
int getMaximumGold(vector<vector<int>>& grid) {
int m = grid.size(),n=grid[0].size();
vector<int> v;
vector<int> vis(m*n,0);
vector<vector<int>> d={{1,0},{-1,0},{0,1},{0,-1}};
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
if(grid[i][j]!=0) {
vis.assign(m*n,0);
int temp = 0;
helper(vis,grid,v,d,i,j,m,n,temp);
}
}
}
auto max_index = max_element(v.begin(),v.end());
return v[max_index-v.begin()];
}
};