Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

혼종 꼬지마루

[백준] 16234 - 인구 이동 본문

Algorithms/BFS

[백준] 16234 - 인구 이동

꼬지마루 2018. 11. 8. 15:00

생각보다 시간이 오래 안걸렸던 문제...


디버깅하느라 시간 오래 쓴줄 알았는데 1시간도 안걸렸다


그냥 문제 조건에 맞게 BFS만 잘 던져주면 쓕하고 풀리는 문제였다.


2. 단지 번호 붙이기 처럼 입력된 조건 범위 내에 차이가 나는 국가들을 BFS로 찾으면서 그 값을 더하고, 갯수를 센다.


3. 함수 종료 전, 찾은 국가들은 check배열에 모두 더한 숫자 / 국가 숫자 로 나누어 갱신


4. map이 check배열과 똑같다면 인구 이동이 없었던 것으로 간주하여 while문 탈출, 아니라면 map을 check배열로 갱신하고, 동시에 check배열을 초기화


이 과정만 잘 구현해 준다면 문제 없이 풀리는 문제이다.


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
#include <iostream>
#define MX 52
#define ABS(X) ((X) > 0? (X) : -(X))
using namespace std;
 
struct PEOPLE {
    int x, y;
}q[MX*MX];
 
int map[MX][MX], check[MX][MX];
int dx[] = { 00-1};
int dy[] = { -110};
int N, L, R, dab;
 
bool mapcom()
{
    for(int i = 0; i<N; i++)
        for (int j = 0; j < N; j++)
            if (map[i][j] != check[i][j]) return true;
    return false;
}
 
void bfs(int x, int y)
{
    int st, ed, ix, iy, nx, ny, cnt, sum;
    st = ed = -1;
    cnt = sum = 0;
    q[++st].x = x; q[st].y = y; cnt++; sum += map[y][x], check[y][x] = 1;
    while (st != ed)
    {
        ix = q[++ed].x; iy = q[ed].y;
        for (int i = 0; i < 4; i++)
        {
            nx = ix + dx[i]; ny = iy + dy[i];
            if (nx < || ny < || nx >= N || ny >= N || check[ny][nx]) continue;
            if (!(L <= ABS(map[iy][ix] - map[ny][nx]) && ABS(map[iy][ix] - map[ny][nx]) <= R)) continue;
            q[++st].x = nx; q[st].y = ny; sum += map[ny][nx]; cnt++; check[ny][nx] = 1;
        }
    }
    for (int i = 0; i <= ed; i++)
        check[q[i].y][q[i].x] = sum / cnt;
}
 
int main(void)
{
    cin >> N >> L >> R;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            cin >> map[i][j];
    while(1)
    {
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                if (!check[i][j])
                    bfs(j, i);
        if (mapcom()) dab++;
        else break;
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
            {
                map[i][j] = check[i][j];
                check[i][j] = 0;
            }
    }
    cout << dab << endl;
    return 0;
}
cs

'Algorithms > BFS' 카테고리의 다른 글

[백준] 17142 - 연구소3  (0) 2019.07.27
[백준] 16236 - 아기 상어  (0) 2018.11.08
[백준] 2667 - 단지번호 붙이기  (0) 2018.10.17
[SWEA] 5521 - 상원이의 생일파티  (0) 2018.09.07
[SWEA] 1267 - 작업순서  (0) 2018.09.03