-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxGrainPath.java
More file actions
62 lines (57 loc) · 1.21 KB
/
MaxGrainPath.java
File metadata and controls
62 lines (57 loc) · 1.21 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
54
55
56
57
58
59
60
61
62
import java.util.*;
import java.lang.Math;
public class MaxGrainPath {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int n, m;
n = kbd.nextInt();
m = kbd.nextInt();
int [][]array = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
array[i][j] = kbd.nextInt();
}
}
int [][]countArray = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
countArray[i][j] = array[i][j];
}
}
int right = 0, bottom = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
right = 0;
bottom = 0;
if (j != m - 1) {
right = countArray[i][j+1];
}
if (i != n - 1) {
bottom = countArray[i+1][j];
}
countArray[i][j] += Math.max(right, bottom);
}
}
int i = 0, j = 0;
int result = array[i][j];
while(i != n - 1 && j != m - 1) {
bottom = 0;
right = 0;
if (i + 1 != n) {
bottom = countArray[i+1][j];
}
if (j + 1 != m) {
right = countArray[i][j+1];
}
if (bottom >= right) {
i = i + 1;
result += array[i][j];
} else {
j = j + 1;
result += array[i][j];
}
}
result += array[n-1][m-1];
System.out.println(result);
}
}