반응형

www.acmicpc.net/problem/16234

 

16234번: 인구 이동

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

www.acmicpc.net

1
2
3
4
5
6
7
8
    bool flag = true;
    while (flag) {
        memset(visited, 0sizeof(visited));
        flag = bfs();
        if(flag)
            ans++;
    }
    cout << ans;
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    rep(k, 4) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        if (nx < 0 || nx >= n || ny < 0 || ny >= n || visited[nx][ny]) continue;
        int gap = abs(arr[x][y] - arr[nx][ny]);        
        if (gap >= l && gap <= r) {    // 차이가 L 이상 R 이하이면
            visited[nx][ny] = 1;    // 방문처리
            q.push({ nx, ny });
            sum += arr[nx][ny];        // 인구수 더하기
            cnt++;                    // 연합 도시 숫자 추가
            v.push_back({ nx, ny });
        }
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 연합 있으면
if (cnt > 1) {
    ret = true;
                    
    int tmp = sum / cnt;
    // 연합 도시 인구수 갱신
    for (pii p : v) {
        int px = p.first;
        int py = p.second;
        arr[px][py] = tmp;
    }
}
sum = 0;
cnt = 0;
v.clear();
cs

 

 

 

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
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#define pii pair<intint>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, l, r, arr[51][51], ans, sum, cnt;
int dx[] = { 010-1 };
int dy[] = { 10-10 };
bool visited[51][51];
queue<pii> q;
vector<pii> v;
bool bfs() {
    bool ret = false;
    rep(i, n) {
        rep(j, n) {
            if (!visited[i][j]) {
                visited[i][j] = 1;
                sum += arr[i][j];
                cnt++;
                v.push_back({ i,j });
                q.push({ i,j });
                while (!q.empty()) {
                    int x = q.front().first;
                    int y = q.front().second;
                    q.pop();
                    rep(k, 4) {
                        int nx = x + dx[k];
                        int ny = y + dy[k];
                        if (nx < 0 || nx >= n || ny < 0 || ny >= n || visited[nx][ny]) continue;
                        int gap = abs(arr[x][y] - arr[nx][ny]);        
                        if (gap >= l && gap <= r) {    // 차이가 L 이상 R 이하이면
                            visited[nx][ny] = 1;    // 방문처리
                            q.push({ nx, ny });
                            sum += arr[nx][ny];        // 인구수 더하기
                            cnt++;                    // 연합 도시 숫자 추가
                            v.push_back({ nx, ny });// 연합 도시 좌표 추가
                        }
                    }
                }
                // 연합 있으면
                if (cnt > 1) {
                    ret = true;
                    
                    int tmp = sum / cnt;
                    // 연합 도시 인구수 갱신
                    for (pii p : v) {
                        int px = p.first;
                        int py = p.second;
                        arr[px][py] = tmp;
                    }
                }
                sum = 0;
                cnt = 0;
                v.clear();
            }
        }
    }
    return ret;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> l >> r;
    rep(i, n) {
        rep(j, n)
            cin >> arr[i][j];
    }
    bool flag = true;
    while (flag) {
        memset(visited, 0sizeof(visited));
        flag = bfs();
        if(flag)
            ans++;
    }
    cout << ans;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 17143  (0) 2021.07.19
백준 5373 [복습필수]  (0) 2021.04.30
백준 15683  (0) 2021.04.30
백준 13458  (0) 2021.04.29
백준 3190  (0) 2021.04.29

+ Recent posts