-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP14_5_1219.java
More file actions
54 lines (46 loc) · 1.54 KB
/
P14_5_1219.java
File metadata and controls
54 lines (46 loc) · 1.54 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class P14_5_1219 {
static int maxGold = 0;
static int[] roww = {1, -1, 0, 0};
static int[] coll = {0, 0, -1, 1};
public static int checkIfAllNonZeros(int[][] grid) {
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] != 0) count += grid[i][j];
else return 0;
}
}
return count;
}
public static void backtrack(int[][] grid, int x, int y, int count) {
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) return;
if (grid[x][y] != 0) {
int curr = grid[x][y];
grid[x][y] = 0;
count += curr;
maxGold = Math.max(maxGold, count);
for (int i = 0; i < 4; i++) {
int newX = x + roww[i];
int newY = y + coll[i];
backtrack(grid, newX, newY, count);
}
grid[x][y] = curr;
}
}
public static int getMaximumGold(int[][] grid) {
int count = checkIfAllNonZeros(grid);
if (count != 0) return count;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] != 0) {
backtrack(grid, i, j, 0);
}
}
}
return maxGold;
}
public static void main(String[] args) {
int[][] grid = {{0,6,0}, {5,8,7},{0,9,0}};
System.out.println(getMaximumGold(grid));
}
}