-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEspressoBucks.java
More file actions
59 lines (49 loc) · 1.66 KB
/
Copy pathEspressoBucks.java
File metadata and controls
59 lines (49 loc) · 1.66 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
// Short circuit evaluation - with multiple conditions, stops checking after 1 is false
// can use to test edge cases
import java.util.Scanner;
public class EspressoBucks {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
char[][] grid = createMatrix(sc.nextInt(), sc.nextInt());
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == '.' && canPlaceCoffeeShop(grid, i, j))
grid[i][j] = 'E';
}
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
System.out.print(grid[i][j]);
}
System.out.println();
}
}
public static char[][] createMatrix(int n, int m) {
char[][] grid = new char[n][m];
String row;
// get rid of whitespace
sc.nextLine();
for (int i = 0; i < grid.length; i++) {
row = sc.nextLine();
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = row.charAt(j);
}
}
return grid;
}
public static boolean canPlaceCoffeeShop(char[][] grid, int row, int col) {
if (row - 1 >= 0 && grid[row - 1][col] == 'E') {
return false;
}
if (row + 1 < grid.length && grid[row + 1][col] == 'E') {
return false;
}
if (col - 1 >= 0 && grid[row][col - 1] == 'E') {
return false;
}
if (col + 1 < grid[0].length && grid[row][col + 1] == 'E') {
return false;
}
return true;
}
}