ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준] 인구 이동 - 16234번
    알고리즘 문제 풀이/백준 2021. 6. 28. 20:31

    문제설명

    https://www.acmicpc.net/problem/16234

     

    16234번: 인구 이동

    N×N크기의 땅이 있고, 땅은 1×1개의 칸으로 나누어져 있다. 각각의 땅에는 나라가 하나씩 존재하며, r행 c열에 있는 나라에는 A[r][c]명이 살고 있다. 인접한 나라 사이에는 국경선이 존재한다. 모

    www.acmicpc.net

     

     

     

    기본아이디어

    시뮬레이션 문제입니다. 나라 갯수가 작기 때문에 BFS 탐색을 통해서 인구 이동이 발생하는 나라 그룹을 만들고 인구이동 시키고 다시 그룹을 만들고 이런 방식으로 구현했습니다.

     

     

    구현코드

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    public static void main(String[] args) throws IOException {
            st = new StringTokenizer(input.readLine());
            N = Integer.parseInt(st.nextToken());
            L = Integer.parseInt(st.nextToken());
            R = Integer.parseInt(st.nextToken());
     
            A = new int[N][N];
            for(int i = 0; i < N; i++){
                int[] temp = new int[N];
                st = new StringTokenizer(input.readLine());
                for(int j = 0; j < N; j++){
                    temp[j] = Integer.parseInt(st.nextToken());
                }
                A[i] = temp;
            }
            System.out.println(move());
        }
        static int N, L, R;
        static int[][] A;
        static boolean[][] isVisited;
        static int move(){
            int ans = -1;
            boolean flag = true;
            while(flag){
                flag = false;
                isVisited = new boolean[N][N];
                for(int i = 0; i < N; i++){
                    for(int j = 0; j < N; j++){
                        if(!isVisited[i][j]){
                            isVisited[i][j] = true;
                            if(bfs(i, j)){
                                flag = true;
                            }
                        }
                    }
                }
                ans++;
            }
            return ans;
        }
     
        static int[] dr = {1-100};
        static int[] dc = {001-1};
        static boolean bfs(int r, int c){
            Queue<int[]> queue = new LinkedList<>();
            List<int[]> list = new ArrayList<>();
            queue.add(new int[]{r,c});
            list.add(new int[]{r,c});
            int sum = A[r][c];
            int count = 1;
            while(!queue.isEmpty()){
                int[] loc = queue.poll();
                for(int d = 0; d < 4; d++){
                    int nr = loc[0+ dr[d];
                    int nc = loc[1+ dc[d];
                    if(isIn(nr, nc) && !isVisited[nr][nc]){
                        int diff = Math.abs(A[nr][nc] - A[loc[0]][loc[1]]);
                        if(diff >= L && diff <= R){
                            isVisited[nr][nc] = true;
                            queue.add(new int[]{nr,nc});
                            list.add(new int[]{nr,nc});
                            sum += A[nr][nc];
                            count++;
                        }
                    }
                }
            }
            if(count == 1){
                return false;
            }
            int population = sum / count;
            for(int[] loc : list){
                A[loc[0]][loc[1]] = population;
            }
            return true;
        }
        static boolean isIn(int r, int c){
            return r >= 0 && r < N && c >= 0 && c < N;
        }
     
    cs

     

    정리, 기억할 내용

    1. 시뮬레이션 문제는 동작을 함수로 나누고, 각 함수가 제대로 실행되는지 테스트 하자

    2. BFS 탐색에서는 방문체크를 어떻게 할 지 고민하자.

     

     

    '알고리즘 문제 풀이 > 백준' 카테고리의 다른 글

    [백준] 탈출 - 3055번  (0) 2021.06.29
    [백준] 주사위 굴리기 - 14499번  (0) 2021.06.29
    [백준] 리모컨 - 1107번  (0) 2021.06.28
    [백준] DSLR - 9019번  (0) 2021.06.23
    [백준] AC - 5430번  (0) 2021.06.23

    댓글

Designed by Tistory.