-
Notifications
You must be signed in to change notification settings - Fork 481
/
1219.cpp
36 lines (34 loc) · 868 Bytes
/
1219.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution
{
public:
int getMaximumGold(vector<vector<int>>& grid)
{
g = grid, m = grid.size(), n = grid[0].size();
int res = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (g[i][j]) res = max(res, g[i][j] + dfs(i, j));
}
}
return res;
}
private:
vector<vector<int>> g;
int m, n;
vector<vector<int>> d = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int dfs(int x, int y)
{
int res = 0, src = g[x][y];
g[x][y] = 0;
for (auto& it : d)
{
int nx = it[0] + x, ny = it[1] + y;
if (nx >= 0 and nx < m and ny >= 0 and ny < n and g[nx][ny])
res = max(res, g[nx][ny] + dfs(nx, ny));
}
g[x][y] = src;
return res;
}
};