- Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathA1.java
101 lines (84 loc) · 3.64 KB
/
A1.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
importjava.util.HashMap;
publicclassA1 {
/*
* The values of the matrix will represent numbers of apples available to the
* gopha in each square of the garden. If the garden does not have an exact
* center, the gopha should start in the square closest to the center with the
* highest apple count.
*
* On a given turn, the gopha will eat the apples available on the square that
* it is on, and then move up, down, left, or right, choosing the square that
* has the most apples. If there are no apples left on any of the adjacent
* squares, the gopha will go to sleep. You may assume that the gopha will never
* have to choose between two squares with the same number of apples.
*/
publicstaticintmaxApplesToPick(int[][] garden) {
intnumApples = 0;
// Find center
Squarecurrent = findCenter(garden);
// Add number of apples to sum, then set to 0 for that square
// Compare surrounding 4 squares to see which has max
// Go to that square and repeat
// Stop when all surrounding squares are 0 or less, and return sum
booleanisMoreApples = true;
HashMap<Integer, Square> hm = newHashMap<>();
while (isMoreApples) {
numApples += current.value;
// current.value = 0;
garden[current.row][current.col] = 0;
intcurrRow = current.row;
intcurrCol = current.col;
intleftApples = numApples(garden, currRow, currCol - 1);
intrightApples = numApples(garden, currRow, currCol + 1);
intupApples = numApples(garden, currRow - 1, currCol);
intdownApples = numApples(garden, currRow + 1, currCol);
// public Square(int value, int row, int col)
hm.put(leftApples, newSquare(leftApples, currRow, currCol - 1));
hm.put(rightApples, newSquare(rightApples, currRow, currCol + 1));
hm.put(upApples, newSquare(upApples, currRow - 1, currCol));
hm.put(downApples, newSquare(downApples, currRow + 1, currCol));
inthorizontalMax = Math.max(hm.get(leftApples).value, hm.get(rightApples).value);
intverticalMax = Math.max(hm.get(upApples).value, hm.get(downApples).value);
intmax = Math.max(horizontalMax, verticalMax);
if (max <= 0) {
isMoreApples = false;
}
current = hm.get(max);
hm.clear();
}
returnnumApples;
}
privatestaticintnumApples(int[][] garden, introw, intcol) {
if (row >= garden.length || row < 0 || col >= garden[0].length || col < 0) {
return -1;
}
returngarden[row][col];
}
publicstaticclassSquare {
privateintvalue;
privateintrow;
privateintcol;
publicSquare(intvalue, introw, intcol) {
this.value = value;
this.row = row;
this.col = col;
}
}
publicstaticSquarefindCenter(int[][] garden) {
// Assume num of columns is always odd
intmidCol = garden[0].length / 2;
intmidRow = garden.length / 2;
if (midRow % 2 == 0) {
if (garden[midRow - 1][midCol] > garden[midRow][midCol]) {
midRow--;
}
}
returnnewSquare(garden[midRow][midCol], midRow, midCol);
}
publicstaticvoidmain(String[] args) {
int[][] garden = newint[][] { { 5, 7, 8, 6, 3 }, { 0, 0, 7, 0, 4 }, { 4, 6, 3, 4, 9 }, { 3, 1, 0, 5, 8 } };
intresult = maxApplesToPick(garden);
System.out.println(result);
return;
}
}