Skip to content

Commit bf5c2f2

Browse files
authored
Merge pull request #619 from AlgorithmWithGod/lkhyun
[20250806] / BOJ / G4 / 인구 이동 / 이강현
2 parents f1b9c7a + 4e00862 commit bf5c2f2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int N,L,R;
10+
static int[][] A;
11+
static int[] di = {0,0,-1,1};
12+
static int[] dj = {-1,1,0,0};
13+
14+
public static void main(String[] args) throws Exception {
15+
st = new StringTokenizer(br.readLine());
16+
N = Integer.parseInt(st.nextToken());
17+
L = Integer.parseInt(st.nextToken());
18+
R = Integer.parseInt(st.nextToken());
19+
20+
A = new int[N][N];
21+
for (int i = 0; i < N; i++) {
22+
st = new StringTokenizer(br.readLine());
23+
for (int j = 0; j < N; j++) {
24+
A[i][j] = Integer.parseInt(st.nextToken());
25+
}
26+
}
27+
int ans = 0;
28+
while(BFS()){
29+
ans++;
30+
}
31+
bw.write(ans+"");
32+
bw.close();
33+
}
34+
static boolean BFS(){
35+
boolean check = false;
36+
boolean[][] visited = new boolean[N][N];
37+
for (int i = 0; i < N; i++) {
38+
for (int j = 0; j < N; j++) {
39+
if(!visited[i][j]){
40+
ArrayDeque<int[]> q = new ArrayDeque<>();
41+
q.offer(new int[]{i,j});
42+
visited[i][j] = true;
43+
44+
int sum = 0;
45+
List<int[]> saveList = new ArrayList<>();
46+
while(!q.isEmpty()){
47+
int[] cur = q.poll();
48+
saveList.add(cur);
49+
50+
sum += A[cur[0]][cur[1]];
51+
52+
for (int k = 0; k < 4; k++) {
53+
int ni = cur[0] + di[k];
54+
int nj = cur[1] + dj[k];
55+
56+
if(ni<0 || ni>=N || nj<0 || nj>=N || visited[ni][nj]) continue;
57+
58+
int cond = Math.abs(A[ni][nj]-A[cur[0]][cur[1]]);
59+
if(cond >= L && cond <= R){
60+
q.offer(new int[]{ni,nj});
61+
visited[ni][nj] = true;
62+
}
63+
}
64+
}
65+
if(saveList.size() > 1){
66+
check = true;
67+
sum = sum/saveList.size();
68+
for (int[] s : saveList) {
69+
A[s[0]][s[1]] = sum;
70+
}
71+
}
72+
}
73+
}
74+
}
75+
return check;
76+
}
77+
}
78+
```

0 commit comments

Comments
 (0)