[ λ°±μ€ / BOJ 16234 ] μΈκ΅¬ μ΄λ ( JAVA )
https://www.acmicpc.net/problem/16234
16234λ²: μΈκ΅¬ μ΄λ
N×Nν¬κΈ°μ λ μ΄ μκ³ , λ μ 1×1κ°μ μΉΈμΌλ‘ λλμ΄μ Έ μλ€. κ°κ°μ λ μλ λλΌκ° νλμ© μ‘΄μ¬νλ©°, rν cμ΄μ μλ λλΌμλ A[r][c]λͺ μ΄ μ΄κ³ μλ€. μΈμ ν λλΌ μ¬μ΄μλ κ΅κ²½μ μ΄ μ‘΄μ¬νλ€. λͺ¨
www.acmicpc.net
λ¬Έμ
N×Nν¬κΈ°μ λ μ΄ μκ³ , λ μ 1×1κ°μ μΉΈμΌλ‘ λλμ΄μ Έ μλ€. κ°κ°μ λ μλ λλΌκ° νλμ© μ‘΄μ¬νλ©°, rν cμ΄μ μλ λλΌμλ A[r][c]λͺ μ΄ μ΄κ³ μλ€. μΈμ ν λλΌ μ¬μ΄μλ κ΅κ²½μ μ΄ μ‘΄μ¬νλ€. λͺ¨λ λλΌλ 1×1 ν¬κΈ°μ΄κΈ° λλ¬Έμ, λͺ¨λ κ΅κ²½μ μ μ μ¬κ°ν ννμ΄λ€.
μ€λλΆν° μΈκ΅¬ μ΄λμ΄ μμλλ λ μ΄λ€.
μΈκ΅¬ μ΄λμ ν루 λμ λ€μκ³Ό κ°μ΄ μ§νλκ³ , λ μ΄μ μλ λ°©λ²μ μν΄ μΈκ΅¬ μ΄λμ΄ μμ λκΉμ§ μ§μλλ€.
- κ΅κ²½μ μ 곡μ νλ λ λλΌμ μΈκ΅¬ μ°¨μ΄κ° Lλͺ μ΄μ, Rλͺ μ΄νλΌλ©΄, λ λλΌκ° 곡μ νλ κ΅κ²½μ μ μ€λ ν루 λμ μ°λ€.
- μμ 쑰건μ μν΄ μ΄μ΄μΌνλ κ΅κ²½μ μ΄ λͺ¨λ μ΄λ Έλ€λ©΄, μΈκ΅¬ μ΄λμ μμνλ€.
- κ΅κ²½μ μ΄ μ΄λ €μμ΄ μΈμ ν μΉΈλ§μ μ΄μ©ν΄ μ΄λν μ μμΌλ©΄, κ·Έ λλΌλ₯Ό μ€λ ν루 λμμ μ°ν©μ΄λΌκ³ νλ€.
- μ°ν©μ μ΄λ£¨κ³ μλ κ° μΉΈμ μΈκ΅¬μλ (μ°ν©μ μΈκ΅¬μ) / (μ°ν©μ μ΄λ£¨κ³ μλ μΉΈμ κ°μ)κ° λλ€. νΈμμ μμμ μ λ²λ¦°λ€.
- μ°ν©μ ν΄μ²΄νκ³ , λͺ¨λ κ΅κ²½μ μ λ«λλ€.
κ° λλΌμ μΈκ΅¬μκ° μ£Όμ΄μ‘μ λ, μΈκ΅¬ μ΄λμ΄ λ©°μΉ λμ λ°μνλμ§ κ΅¬νλ νλ‘κ·Έλ¨μ μμ±νμμ€.
μ λ ₯
첫째 μ€μ N, L, Rμ΄ μ£Όμ΄μ§λ€. (1 \(\leq\)N \(\leq\)50, 1 \(\leq\) L \(\leq\) R \(\leq\) 100)
λμ§Έ μ€λΆν° Nκ°μ μ€μ κ° λλΌμ μΈκ΅¬μκ° μ£Όμ΄μ§λ€. rν cμ΄μ μ£Όμ΄μ§λ μ μλ A[r][c]μ κ°μ΄λ€. (0 \(\leq\) A[r][c] \(\leq\) 100)
μΈκ΅¬ μ΄λμ΄ λ°μνλ μΌμκ° 2,000λ² λ³΄λ€ μκ±°λ κ°μ μ λ ₯λ§ μ£Όμ΄μ§λ€.
μΆλ ₯
μΈκ΅¬ μ΄λμ΄ λ©°μΉ λμ λ°μνλμ§ μ²«μ§Έ μ€μ μΆλ ₯νλ€.
νμ΄
BFS
λ₯Ό μ΄μ©ν΄ νμ΄
solution()
: κ° λλΌλ₯Ό λλ©΄μ κ΅κ²½μ μ΄ μ΄λ Έλμ§ νμΈ
β¬οΈisOpen(int i, int j)
- BFSλ‘ μΈμ ν λλΌλ€κ³Ό μΈκ΅¬ μκ° Lμ΄μ Rμ΄νμ΄λ©΄ κ΅κ²½μ μ μ€ν
- union Listμ μΆκ°
- μΈκ΅¬ μλ₯Ό population / union.size()λ‘ λ³κ²½
μ½λ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_BOJ_16234_μΈκ΅¬μ΄λ {
static int N, L, R;
static int[][] countries;
static boolean[][] isVisited;
static int[] dx = {-1, 0, 0, 1};
static int[] dy = {0, -1, 1, 0};
static Queue<int[]> queue;
static List<int[]> union;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
N = Integer.parseInt(stringTokenizer.nextToken());
L = Integer.parseInt(stringTokenizer.nextToken());
R = Integer.parseInt(stringTokenizer.nextToken());
countries = new int[N][N];
for (int i = 0; i < N; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
countries[i][j] = Integer.parseInt(stringTokenizer.nextToken());
}
}
int result = solution();
System.out.println(result);
}
private static int solution() {
int cnt = 0;
boolean isMove;
while (true) {
isVisited = new boolean[N][N];
isMove = false;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(isVisited[i][j]) continue;
if(isOpen(i,j)) isMove = true;
}
}
if(isMove) cnt++;
else return cnt;
}
}
private static boolean isOpen(int i, int j) {
queue = new LinkedList<>();
union = new ArrayList<>();
queue.add(new int[]{i, j});
union.add(new int[]{i, j});
isVisited[i][j] = true;
int population = countries[i][j];
while (!queue.isEmpty()) {
int[] now = queue.poll();
for (int d = 0; d < 4; d++) {
int nx = now[0] + dx[d];
int ny = now[1] + dy[d];
if (nx < 0 || N <= nx || ny < 0 || N <= ny || isVisited[nx][ny]) continue;
int dif = Math.abs(countries[now[0]][now[1]] - countries[nx][ny]);
if(dif < L || R < dif) continue;
population += countries[nx][ny];
queue.add(new int[]{nx, ny});
union.add(new int[]{nx, ny});
isVisited[nx][ny] = true;
}
}
if(union.size() < 2) return false;
int tmp = population / union.size();
for (int[] u : union) {
countries[u[0]][u[1]] = tmp;
}
return true;
}
}