Skip to content

Commit 21645af

Browse files
authored
Merge pull request #480 from AlgorithmWithGod/03do-new30
[20250716] BOJ / G5 / 경쟁적 전염 / 신동윤
2 parents c18405b + 19dc5f3 commit 21645af

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
static int N, K, S, X, Y;
7+
static int[][] arr;
8+
static int[] dr = {0, 0, -1, 1};
9+
static int[] dc = {-1, 1, 0, 0};
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
N = Integer.parseInt(st.nextToken());
14+
K = Integer.parseInt(st.nextToken());
15+
arr = new int[N+1][N+1];
16+
for (int i = 1; i <= N; i++) {
17+
st = new StringTokenizer(br.readLine());
18+
for (int j = 1; j <= N; j++) {
19+
arr[i][j] = Integer.parseInt(st.nextToken());
20+
}
21+
}
22+
st = new StringTokenizer(br.readLine());
23+
S = Integer.parseInt(st.nextToken());
24+
X = Integer.parseInt(st.nextToken());
25+
Y = Integer.parseInt(st.nextToken());
26+
br.close();
27+
28+
// bfs
29+
bfs();
30+
System.out.println(arr[X][Y]);
31+
32+
}
33+
private static void bfs() {
34+
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() { // (바이러스가 퍼진 시간, 바이러스 번호, r, c)
35+
@Override
36+
public int compare(int[] o1, int[] o2) {
37+
if (Integer.compare(o1[0], o2[0]) == 0) {
38+
return Integer.compare(o1[1], o2[1]);
39+
} else {
40+
return Integer.compare(o1[0], o2[0]);
41+
}
42+
}
43+
});
44+
45+
for (int i = 1; i <= N; i++) {
46+
for (int j = 1; j <= N; j++) {
47+
if (arr[i][j] > 0) {
48+
pq.offer(new int[] {0, arr[i][j], i, j});
49+
}
50+
}
51+
}
52+
53+
while (!pq.isEmpty()) {
54+
int[] info = pq.poll();
55+
int curTime = info[0];
56+
int curVirus = info[1];
57+
int r = info[2];
58+
int c = info[3];
59+
60+
if (curTime == S) continue;
61+
for (int i = 0; i < 4; i++) {
62+
int nr = r + dr[i];
63+
int nc = c + dc[i];
64+
if (nr < 1 || nr > N || nc < 1 || nc > N) {
65+
continue;
66+
}
67+
if (arr[nr][nc] > 0) continue;
68+
arr[nr][nc] = curVirus;
69+
pq.offer(new int[] {curTime + 1, curVirus, nr, nc});
70+
}
71+
}
72+
}
73+
74+
}
75+
```

0 commit comments

Comments
 (0)