-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialDistancing.java
More file actions
78 lines (64 loc) · 2.06 KB
/
Copy pathSocialDistancing.java
File metadata and controls
78 lines (64 loc) · 2.06 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package basic;
public class SocialDistancing {
private static final int[] dx = {0, -1, 1, 0};
private static final int[] dy = {-1, 0, 0, 1};
private static boolean isNextToVolunteer(char[][] room, int x, int y, int exclude) {
for (int d = 0; d < 4; d++) {
if (d == exclude)
continue;
int nx = x + dx[d];
int ny = y + dy[d];
if (ny < 0 || ny >= room.length || nx < 0 || nx >= room[ny].length)
continue;
if (room[ny][nx] == 'P') {
return true;
}
}
return false;
}
private static boolean isDistanced(char[][] room, int x, int y) {
for (int d = 0; d < 4; d++) {
int nx = x + dx[d];
int ny = y + dy[d];
if (ny < 0 || ny >= room.length || nx < 0 || nx >= room[ny].length)
continue;
char c = room[ny][nx];
if (c == 'P') {
return false;
}
if (c == 'O') {
if(isNextToVolunteer(room, nx, ny, 3 - d))
return false;
}
}
return true;
}
public static boolean isDistanced(char[][] room) {
for (int y = 0; y < room.length; y++) {
for (int x = 0; x < room[y].length; x++) {
if (room[y][x] == 'P') {
if (!isDistanced(room, x, y)) {
return false;
}
}
}
}
return true;
}
public static int[] solution(String[][] places) {
int[] result = new int[places.length];
for (int i = 0; i < places.length; i++) {
String[] place = places[i];
char[][] room = new char[place.length][];
for (int y = 0; y < room.length; y++) {
room[y] = place[y].toCharArray();
}
if(isDistanced(room)) {
result[i] = 1;
continue;
}
result[i] = 0;
}
return result;
}
}