Skip to content

Commit 430f3e3

Browse files
authored
[20251010] PGM / LV2 / 거리두기 확인하기 / 김수연
1 parent 2729898 commit 430f3e3

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
static int[] dx = {-1, 1, 0, 0};
6+
static int[] dy = {0, 0, -1, 1};
7+
static List<Integer> answer = new ArrayList<>();
8+
9+
public List<Integer> solution(String[][] places) {
10+
for (String[] place : places) {
11+
String[] p = place;
12+
boolean isfalse = false;
13+
outer:for (int i = 0; i < 5; i++) {
14+
for (int j = 0; j < 5; j++) {
15+
if (p[i].charAt(j) == 'P') {
16+
if(!bfs(i, j, p)) {
17+
System.out.println(i + " " + j);
18+
isfalse = true;
19+
break outer;
20+
}
21+
}
22+
}
23+
}
24+
if (isfalse) {
25+
answer.add(0);
26+
} else {
27+
answer.add(1);
28+
}
29+
}
30+
return answer;
31+
}
32+
33+
static boolean bfs(int i, int j, String[] p) {
34+
Queue<int[]> queue = new ArrayDeque<>();
35+
queue.offer(new int[]{i, j});
36+
boolean[][] visited = new boolean[5][5];
37+
visited[i][j] = true;
38+
39+
while(!queue.isEmpty()) {
40+
int[] cur = queue.poll();
41+
for (int n = 0; n < 4; n++) {
42+
int nx = cur[0] + dx[n];
43+
int ny = cur[1] + dy[n];
44+
45+
if (nx < 0 || ny < 0 || nx >= 5 || ny >= 5) continue;
46+
47+
if (!visited[nx][ny]) {
48+
int d = Math.abs(nx - i) + Math.abs(ny - j);
49+
50+
if (d <= 2 && p[nx].charAt(ny) == 'P') {
51+
return false;
52+
} else if(p[nx].charAt(ny) == 'O' && d <= 2) {
53+
System.out.println(nx + " " + ny);
54+
visited[nx][ny] = true;
55+
queue.offer(new int[]{nx, ny});
56+
}
57+
}
58+
}
59+
}
60+
System.out.println(" ");
61+
return true;
62+
}
63+
}
64+
```

0 commit comments

Comments
 (0)