-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCherryPickup.java
198 lines (183 loc) · 5.78 KB
/
CherryPickup.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package techQuestions;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
public class CherryPickup {
public static void main(String[] args) {
int[][] grid = {{0,1,-1},{1,0,-1},{1,1,1}};
System.out.println(Arrays.deepToString(grid).replace("], ", "]\n"));
System.out.println(cherryField(grid));
}
public static int cherryField(int[][] grid) {
int cherriesCollected = 0;
int rows = grid.length;
int columns = grid[0].length;
Queue<int []> cherries = new LinkedList<>();
HashMap<Integer, Integer> thorns = new HashMap<Integer, Integer>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (grid[i][j] == -1) {
thorns.put(i, j);
} else if (grid[i][j] == 1) {
cherries.add(new int[] {i, j});
}
}
}
// for (Map.Entry<Integer, Integer> entry : thorns.entrySet()) {
// System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
// }
//
// // using Iterator to iterate through Queue
// Iterator<int[]> itr = cherries.iterator();
//
// // hasNext() returns true if the queue has more elements
// while (itr.hasNext())
// {
// // next() returns the next element in the iteration
// int[] cherry_coordinates = itr.next();
// // print x and y coordinates of each cherry location
// System.out.println(cherry_coordinates[0] + ", " + cherry_coordinates[1]);
// }
int[][] dirs = {{1,0},{0,1}};
System.out.println("-----");
int i = 0, j = 0;
while (i < rows && j < columns) {
try {
if (grid[i][j+1] == 1 && isValidMove(rows, i, j, "right") == true) {
// if right move has cherry, move right
grid[i][j+1] = 0;
j++;
cherriesCollected++;
} else if (grid[i+1][j] == 1 && isValidMove(rows, i, j, "down") == true) {
// else if down move has cherry, move down
grid[i+1][j] = 0;
i++;
cherriesCollected++;
} else if (grid[i][j+1] == -1 && grid[i+1][j] == -1) {
// else if both right and down are thorns, return error
return 999;
} else if (grid[i][j+1] == -1) {
// else if right move have thorn, move down
i++;
if (grid[i][j] == 1) {
cherriesCollected++;
grid[i][j] = 0;
}
} else {
// choose right move over down move if both are 0
j++;
if (grid[i][j] == 1) {
cherriesCollected++;
grid[i][j] = 0;
}
}
// print new grid at every turn
System.out.println(Arrays.deepToString(grid).replace("], ", "]\n"));
System.out.println("i is: " + i + " ----- j is:" + j + " ---- count is: " + cherriesCollected);
} catch (ArrayIndexOutOfBoundsException exception) {
System.out.println("we have reached the bottom right");
break;
}
}
System.out.println("we have reached the bottom right");
i = rows - 1;
j = columns - 1;
while (i >= 0 && j >= 0) {
try {
// if we are at the far left, can only move up!
if (j == 0) {
if (grid[i-1][j] == -1) {
// thorn above! return error!
return 999;
}
i--;
if (grid[i][j] == 1) {
cherriesCollected++;
grid[i][j] = 0;
}
// if we are at the top, can only move left
} else if (i == 0) {
if (grid[i][j-1] == -1) {
// thorn above! return error!
return 999;
}
j--;
if (grid[i][j] == 1) {
cherriesCollected++;
grid[i][j] = 0;
}
}
else if (grid[i][j-1] == 1 && isValidMove(rows, i, j, "left") == true) {
// if left move has cherry, move left
grid[i][j-1] = 0;
j--;
cherriesCollected++;
} else if (grid[i-1][j] == 1 && isValidMove(rows, i, j, "up") == true) {
// else if up move has cherry, move up
grid[i-1][j] = 0;
i--;
cherriesCollected++;
} else if (grid[i][j-1] == -1 && grid[i-1][j] == -1) {
// else if both left and up are thorns, return error
return 999;
} else if (grid[i][j-1] == -1 && isValidMove(rows, i, j, "up") == true) {
// else if left move has thorn, move up
i--;
if (grid[i][j] == 1) {
cherriesCollected++;
grid[i][j] = 0;
}
} else if (j == 0 && isValidMove(rows, i, j, "up") == true) {
// hit left most bound, only move up now
if (grid[i-1][j] == -1) {
return 999;
}
i--;
if (grid[i][j] == 1) {
cherriesCollected++;
grid[i][j] = 0;
}
} else if (isValidMove(rows, i, j, "left") == true) {
// choose left move over up move if both are 0
j--;
if (grid[i][j] == 1) {
cherriesCollected++;
grid[i][j] = 0;
}
}
// print new grid at every turn
System.out.println(Arrays.deepToString(grid).replace("], ", "]\n"));
System.out.println("i is: " + i + " ----- j is:" + j + " ---- count is: " + cherriesCollected);
} catch (ArrayIndexOutOfBoundsException exception) {
System.out.println("we have reached the top left");
break;
}
}
System.out.println("we have reached the top left");
// System.out.println(cherries.iterator());
// System.out.println(thorns.iterator());
return cherriesCollected;
}
public static boolean isValidMove(int N, int row, int column, String move) {
System.out.println("is Valid has been called! input is: " + N + row + column + move);
if (row == 0 && move == "up") {
System.out.println("at row = 0 and trying to move up!");
return false;
} else if (column == 0 && move == "left") {
System.out.println("at column = 0 and trying to move left!");
return false;
} else if (row == N && move == "down") {
System.out.println("at row = nax and trying to move down!");
return false;
} else if (column == N && move == "right") {
System.out.println("at column = max and trying to move right!");
return false;
} else {
return true;
}
}
}