forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1219.js
More file actions
24 lines (22 loc) · 710 Bytes
/
Copy path1219.js
File metadata and controls
24 lines (22 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var getMaximumGold = function(grid) {
let m = grid.length, n = grid[0].length, res = 0;
let d = [[0, 1], [1, 0], [0, -1], [-1, 0]];
let dfs = function(x, y) {
let res = 0, src = grid[x][y];
grid[x][y] = 0;
for (let [i, j] of d) {
let nx = x + i, ny = y + j;
if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny]) {
res = Math.max(res, dfs(nx, ny) + grid[nx][ny]);
}
}
grid[x][y] = src;
return res;
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j]) res = Math.max(res, dfs(i, j) + grid[i][j]);
}
}
return res;
};