프로그래밍 공방

[백준] 16234번 : 인구 이동 본문

개발/문제해결

[백준] 16234번 : 인구 이동

hyosupsong 2020. 12. 17. 22:06

문제

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
81
package baekjoon;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
 
public class Main16234_인구이동 {
 
    public static int N, min, max;
    public static boolean[][] v;
    public static int[][] map;
    public static int[][] dir = {{10}, {-10}, {01}, {0-1}};
    
    public static boolean bfs(int i, int j, boolean check) {
        boolean m = false;
        List<int[]> list = new ArrayList<>();
        int index = 0;
        list.add(new int[] {i, j});
        v[i][j] = check;
        int count = 1;
        int sum = map[i][j];
        while(index!=list.size()) {
            int[] cur = list.get(index++);
            for(int d=0; d<dir.length; d++) {
                int x = cur[0+ dir[d][0];
                int y = cur[1+ dir[d][1];
                if(x>=0 && x<&& y>=0 && y<&& v[x][y]!=check) {
                    int diff = Math.abs(map[x][y] - map[cur[0]][cur[1]]);
                    if(diff>=min && diff<=max) {
                        v[x][y] = check;
                        count++;
                        sum += map[x][y];
                        list.add(new int[] {x, y});
                    }
                }
            }
        }
        for(int s=0; s<list.size(); s++) {
            int[] cur = list.get(s);
            map[cur[0]][cur[1]] = sum/count;
        }
        if(list.size()>1) m = true;
        return m;
    }
    
    public static boolean isMove() {
        boolean m = false;
        boolean check = !v[0][0];
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                if(v[i][j]!=check) {
                    m |= bfs(i, j, check);
                }
            }
        }
        return m;
    }
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        v = new boolean[N][N];
        map = new int[N][N];
        min = Integer.parseInt(st.nextToken());
        max = Integer.parseInt(st.nextToken());
        for(int i=0; i<N; i++) {
            st = new StringTokenizer(br.readLine());
            for(int j=0; j<N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        int count = 0;
        while(isMove()) {
            count++;
        }
        System.out.println(count);
    }
}
cs


코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.