|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +class Position { |
| 4 | + private int x; |
| 5 | + private int y; |
| 6 | + |
| 7 | + public Position(int x, int y) { |
| 8 | + this.x = x; |
| 9 | + this.y = y; |
| 10 | + } |
| 11 | + |
| 12 | + public int getX() { |
| 13 | + return this.x; |
| 14 | + } |
| 15 | + |
| 16 | + public int getY() { |
| 17 | + return this.y; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +public class Main { |
| 22 | + // 땅의 크기(N), L, R 값을 입력받기 |
| 23 | + public static int n, l, r; |
| 24 | + public static int totalCount = 0; |
| 25 | + |
| 26 | + // 전체 나라의 정보(N x N)를 입력받기 |
| 27 | + public static int[][] graph = new int[50][50]; |
| 28 | + public static int[][] unions = new int[50][50]; |
| 29 | + |
| 30 | + public static int[] dx = {-1, 0, 1, 0}; |
| 31 | + public static int[] dy = {0, -1, 0, 1}; |
| 32 | + |
| 33 | + // 특정 위치에서 출발하여 모든 연합을 체크한 뒤에 데이터 갱신 |
| 34 | + public static void process(int x, int y, int index) { |
| 35 | + // (x, y)의 위치와 연결된 나라(연합) 정보를 담는 리스트 |
| 36 | + ArrayList<Position> united = new ArrayList<>(); |
| 37 | + united.add(new Position(x, y)); |
| 38 | + // 너비 우선 탐색 (BFS)을 위한 큐 라이브러리 사용 |
| 39 | + Queue<Position> q = new LinkedList<>(); |
| 40 | + q.offer(new Position(x, y)); |
| 41 | + unions[x][y] = index; // 현재 연합의 번호 할당 |
| 42 | + int summary = graph[x][y]; // 현재 연합의 전체 인구 수 |
| 43 | + int count = 1; // 현재 연합의 국가 수 |
| 44 | + // 큐가 빌 때까지 반복(BFS) |
| 45 | + while (!q.isEmpty()) { |
| 46 | + Position pos = q.poll(); |
| 47 | + x = pos.getX(); |
| 48 | + y = pos.getY(); |
| 49 | + // 현재 위치에서 4가지 방향을 확인하며 |
| 50 | + for (int i = 0; i < 4; i++) { |
| 51 | + int nx = x + dx[i]; |
| 52 | + int ny = y + dy[i]; |
| 53 | + // 바로 옆에 있는 나라를 확인하여 |
| 54 | + if (0 <= nx && nx < n && 0 <= ny && ny < n && unions[nx][ny] == -1) { |
| 55 | + // 옆에 있는 나라와 인구 차이가 L명 이상, R명 이하라면 |
| 56 | + int gap = Math.abs(graph[nx][ny] - graph[x][y]); |
| 57 | + if (l <= gap && gap <= r) { |
| 58 | + q.offer(new Position(nx, ny)); |
| 59 | + // 연합에 추가하기 |
| 60 | + unions[nx][ny] = index; |
| 61 | + summary += graph[nx][ny]; |
| 62 | + count += 1; |
| 63 | + united.add(new Position(nx, ny)); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + // 연합 국가끼리 인구를 분배 |
| 69 | + for (int i = 0; i < united.size(); i++) { |
| 70 | + x = united.get(i).getX(); |
| 71 | + y = united.get(i).getY(); |
| 72 | + graph[x][y] = summary / count; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static void main(String[] args) { |
| 77 | + Scanner sc = new Scanner(System.in); |
| 78 | + |
| 79 | + n = sc.nextInt(); |
| 80 | + l = sc.nextInt(); |
| 81 | + r = sc.nextInt(); |
| 82 | + |
| 83 | + for (int i = 0; i < n; i++) { |
| 84 | + for (int j = 0; j < n; j++) { |
| 85 | + graph[i][j] = sc.nextInt(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // 더 이상 인구 이동을 할 수 없을 때까지 반복 |
| 90 | + while (true) { |
| 91 | + for (int i = 0; i < n; i++) { |
| 92 | + for (int j = 0; j < n; j++) { |
| 93 | + unions[i][j] = -1; |
| 94 | + } |
| 95 | + } |
| 96 | + int index = 0; |
| 97 | + for (int i = 0; i < n; i++) { |
| 98 | + for (int j = 0; j < n; j++) { |
| 99 | + if (unions[i][j] == -1) { // 해당 나라가 아직 처리되지 않았다면 |
| 100 | + process(i, j, index); |
| 101 | + index += 1; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + // 모든 인구 이동이 끝난 경우 |
| 106 | + if (index == n * n) break; |
| 107 | + totalCount += 1; |
| 108 | + } |
| 109 | + |
| 110 | + // 인구 이동 횟수 출력 |
| 111 | + System.out.println(totalCount); |
| 112 | + } |
| 113 | +} |
0 commit comments