Skip to content

Commit 723d1af

Browse files
authored
[20250918] PGM / LV2 / 지게차와 크레인 / 김수연
[20250918] PGM / LV2 / 지게차와 크레인 / 김수연
2 parents 931d7a6 + e97b437 commit 723d1af

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
```java
2+
import java.util.*;
3+
class Solution {
4+
static int[][] dir = new int[][] {{1,0}, {-1,0}, {0,1}, {0,-1}};
5+
static int N, M;
6+
static char[][] stor;
7+
public int solution(String[] storage, String[] requests) {
8+
int answer = 0;
9+
N = storage.length;
10+
M = storage[0].length();
11+
stor = new char[N][M];
12+
for (int i = 0; i < N; i++) {
13+
for (int j = 0; j < M; j++) {
14+
stor[i][j] = storage[i].charAt(j);
15+
}
16+
}
17+
for (String request : requests) {
18+
int type = request.length();
19+
if (type == 1) {
20+
ArrayList<int[]> arr = new ArrayList<>();
21+
for (int i = 0; i < N; i++) {
22+
for (int j = 0; j < M; j++) {
23+
if (stor[i][j] != request.charAt(0)) continue;
24+
if (i == 0 || i == N-1 || j == 0 || j == M-1 || check(i, j)) arr.add(new int[]{i, j});
25+
}
26+
}
27+
for (int[] a : arr) stor[a[0]][a[1]] = '0';
28+
} else if (type == 2) {
29+
for (int i = 0; i < N; i++) {
30+
for (int j = 0; j < M; j++) {
31+
if (stor[i][j] == request.charAt(0)) stor[i][j] = '0';
32+
}
33+
}
34+
}
35+
}
36+
for (int i = 0; i < N; i++) {
37+
for (int j = 0; j < M; j++) {
38+
if (stor[i][j] != '0') answer++;
39+
}
40+
}
41+
return answer;
42+
}
43+
44+
static boolean check(int i, int j) {
45+
boolean[][] visited = new boolean[N][M];
46+
Queue<int[]> q = new LinkedList<>();
47+
q.add(new int[] {i, j});
48+
visited[i][j] = true;
49+
while (!q.isEmpty()) {
50+
int[] curr = q.poll();
51+
for (int[] d : dir) {
52+
int ny = curr[0] + d[0];
53+
int nx = curr[1] + d[1];
54+
if (ny < 0 || ny >= N || nx < 0 || nx >= M || visited[ny][nx] || stor[ny][nx] != '0') continue;
55+
if (ny == 0 || ny == N-1 || nx == 0 || nx == M-1) return true;
56+
q.add(new int[] {ny, nx});
57+
visited[ny][nx] = true;
58+
}
59+
}
60+
return false;
61+
}
62+
}
63+
```

0 commit comments

Comments
 (0)