๐ก Info
๋ด์ฉ
NรNํฌ๊ธฐ์ ๋ ์ด ์๊ณ , ๋ ์ 1ร1๊ฐ์ ์นธ์ผ๋ก ๋๋์ด์ ธ ์๋ค. ๊ฐ๊ฐ์ ๋ ์๋ ๋๋ผ๊ฐ ํ๋์ฉ ์กด์ฌํ๋ฉฐ, rํ c์ด์ ์๋ ๋๋ผ์๋ A[r][c]๋ช ์ด ์ด๊ณ ์๋ค. ์ธ์ ํ ๋๋ผ ์ฌ์ด์๋ ๊ตญ๊ฒฝ์ ์ด ์กด์ฌํ๋ค. ๋ชจ๋ ๋๋ผ๋ 1ร1 ํฌ๊ธฐ์ด๊ธฐ ๋๋ฌธ์, ๋ชจ๋ ๊ตญ๊ฒฝ์ ์ ์ ์ฌ๊ฐํ ํํ์ด๋ค.
์ค๋๋ถํฐ ์ธ๊ตฌ ์ด๋์ด ์์๋๋ ๋ ์ด๋ค.
์ธ๊ตฌ ์ด๋์ ํ๋ฃจ ๋์ ๋ค์๊ณผ ๊ฐ์ด ์งํ๋๊ณ , ๋ ์ด์ ์๋ ๋ฐฉ๋ฒ์ ์ํด ์ธ๊ตฌ ์ด๋์ด ์์ ๋๊น์ง ์ง์๋๋ค.
๊ฐ ๋๋ผ์ ์ธ๊ตฌ์๊ฐ ์ฃผ์ด์ก์ ๋, ์ธ๊ตฌ ์ด๋์ด ๋ฉฐ์น ๋์ ๋ฐ์ํ๋์ง ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
๐ฅ์ ๋ ฅ ์กฐ๊ฑด
๐ค์ถ๋ ฅ ์กฐ๊ฑด
โซ๏ธ ์ ์ถ๋ ฅ ์์
2 20 50
50 30
20 40
1
2 40 50
50 30
20 40
0
๊ฒฝ๊ณ๋ฅผ ๊ณต์ ํ๋ ๋๋ผ์ ์ธ๊ตฌ ์ฐจ์ด๊ฐ ๋ชจ๋ L๋ณด๋ค ์์์ ์ธ๊ตฌ ์ด๋์ด ๋ฐ์ํ์ง ์๋๋ค.
2 20 50
50 30
30 40
1
3 5 10
10 15 20
20 30 25
40 22 10
2
4 10 50
10 100 20 90
80 100 60 70
70 20 30 40
50 20 100 10
3
์ค์ ํ์ด ์๊ฐ : 40๋ถ
์ ๋ ฅ
๊ณ์ฐ
์ถ๋ ฅ
๋ณ๋ ๋ฉ์๋
import java.util.*;
public class Main {
static int N;
static int L;
static int R;
static int[][] arr;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
L = sc.nextInt();
R = sc.nextInt();
sc.nextLine();
arr = new int[N][N];
for(int i=0; i<N; i++){
String land = sc.nextLine();
for(int j=0; j<N; j++){
arr[i][j] = land.charAt(j) - '0';
}
}
int result = 0;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(dfs(i,j)) {
result++;
}
}
}
System.out.println(result);
}
public static boolean dfs(int i, int j) {
if (i <= -1 || i >= N || j <= -1 || j >= N) {//ํ ๋ฐ์ผ๋ก ๋๊ฐ ๊ฒฝ์ฐ
return false;
}
if (arr[i][j] == 0) {
arr[i][j] = 1; //๋ฐฉ๋ฌธ
dfs(i + 1, j);
dfs(i - 1, j);
dfs(i, j + 1);
dfs(i, j - 1);
return true;
}
return false;
}
}
public static int bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
union = new ArrayList<>();
queue.offer(new int[]{x, y});
visited[x][y] = true;
int sum = arr[x][y];
while (!queue.isEmpty()) {
int[] current = queue.poll();
x = current[0];
y = current[1];
union.add(current);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx][ny]) {
int population = Math.abs(arr[x][y] - arr[nx][ny]);
if (L <= population && population <= R) {
queue.offer(new int[]{nx, ny});
visited[nx][ny] = true;
sum += arr[nx][ny];
}
}
}
}
return sum;
}
public static int move() {
int result = 0;
while (true) {
boolean isMove = false;
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j]) {
int sum = bfs(i, j);
if (union.size() > 1) {
change(sum);
isMove = true;
}
}
}
}
if (!isMove) return result;
result++;
}
}
public static void change(int sum) {
int average = sum / union.size();
for (int[] coord : union) {
arr[coord[0]][coord[1]] = average;
}
}
์ค์ ํ์ด ์๊ฐ : 1์๊ฐ 20๋ถ(์ฒซ ํ์ด ์๊ฐ ํฌํจ)
์ ๋ ฅ
๊ณ์ฐ
์ถ๋ ฅ
๋ณ๋ ๋ฉ์๋
import java.util.*;
public class Main {
static int N;
static int L;
static int R;
static int[][] arr;
static boolean[][] visited;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static ArrayList<int[]> union;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
L = sc.nextInt();
R = sc.nextInt();
sc.nextLine();
arr = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.println(move());
}
public static int bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
union = new ArrayList<>();
queue.offer(new int[]{x, y});
visited[x][y] = true;
int sum = arr[x][y];
while (!queue.isEmpty()) {
int[] current = queue.poll();
x = current[0];
y = current[1];
union.add(current);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx][ny]) {
int population = Math.abs(arr[x][y] - arr[nx][ny]);
if (L <= population && population <= R) {
queue.offer(new int[]{nx, ny});
visited[nx][ny] = true;
sum += arr[nx][ny];
}
}
}
}
return sum;
}
public static int move() {
int result = 0;
while (true) {
boolean isMove = false;
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j]) {
int sum = bfs(i, j);
if (union.size() > 1) {
change(sum);
isMove = true;
}
}
}
}
if (!isMove) return result;
result++;
}
}
public static void change(int sum) {
int average = sum / union.size();
for (int[] coord : union) {
arr[coord[0]][coord[1]] = average;
}
}
}