๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 16234 : ์ธ๊ตฌ ์ด๋™

by Jaeguk 2022. 5. 10.
๋ฌธ์ œ ๋งํฌ

16234๋ฒˆ: ์ธ๊ตฌ ์ด๋™ (acmicpc.net)

 

16234๋ฒˆ: ์ธ๊ตฌ ์ด๋™

N×Nํฌ๊ธฐ์˜ ๋•…์ด ์žˆ๊ณ , ๋•…์€ 1×1๊ฐœ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋•…์—๋Š” ๋‚˜๋ผ๊ฐ€ ํ•˜๋‚˜์”ฉ ์กด์žฌํ•˜๋ฉฐ, rํ–‰ c์—ด์— ์žˆ๋Š” ๋‚˜๋ผ์—๋Š” A[r][c]๋ช…์ด ์‚ด๊ณ  ์žˆ๋‹ค. ์ธ์ ‘ํ•œ ๋‚˜๋ผ ์‚ฌ์ด์—๋Š” ๊ตญ๊ฒฝ์„ ์ด ์กด์žฌํ•œ๋‹ค. ๋ชจ

www.acmicpc.net

BFS๋ฅผ ์ด์šฉํ•œ ๊ตฌํ˜„๋ฌธ์ œ.

ํ’€์ด

์ž„์˜์˜ ๋‚˜๋ผ์—์„œ ์ธ์ ‘ํ•œ ๋‚˜๋ผ์™€ ์ธ๊ตฌ ์ˆ˜๊ฐ€ L์ด์ƒ R์ดํ•˜ ๋งŒํผ ์ฐจ์ด๋‚˜๋ฉด ๊ตญ๊ฒฝ์„ ๊ณต์œ ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ตญ๊ฒฝ์„ ๊ณต์œ ํ•œ ๋‚˜๋ผ๋“ค์€ ์—ฐํ•ฉ์ด๋ผ๊ณ  ํ•˜๊ณ  ์—ฐํ•ฉ๊ตญ๋“ค์˜ ์ธ๊ตฌ๋Š” (์—ฐํ•ฉ๊ตญ์˜ ์ด ์ธ๊ตฌ์ˆ˜ / ์—ฐํ•ฉ๊ตญ์˜ ์ˆ˜) ๋กœ ํ†ต์ผ๋œ๋‹ค.

๊ฐ ๋‚˜๋ผ๋ฅผ ๋Œ๋ฉด์„œ ์ž„์˜์˜ ๋‚˜๋ผ์˜ ์ธ์ ‘๊ตญ์ค‘์— ์ธ๊ตฌ์ฐจ์ด๊ฐ€ L์ด์ƒ R์ดํ•˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ธ์ ‘๊ตญ์ด ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด ๊ทธ ์ž„์˜์˜ ๋‚˜๋ผ์—์„œ BFSํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค. BFS๋Š” ์ธ์ ‘ํ•œ ๋‚˜๋ผ ์ค‘ ์ธ๊ตฌ์ฐจ์ด๊ฐ€ L์ด์ƒ R์ดํ•˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ธ์ ‘๊ตญ์œผ๋กœ๋งŒ ์ด๋™ํ•œ๋‹ค

ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋ชจ๋“  ์—ฐํ•ฉ๊ตญ์˜ ์ขŒํ‘œ๋ฅผ ๋ฒกํ„ฐ์— ๋„ฃ์–ด๋‘๊ณ  ๋งˆ์ง€๋ง‰์— ์ธ๊ตฌ๋ฅผ ํ†ต์ผ์‹œ์ผฐ๋‹ค.

๊ทธ๋ฆฌ๊ณ  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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 50 + 5;
int A[MAX_N][MAX_N];
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
int N, L, R;
bool visited[MAX_N][MAX_N];
 
bool is_ok(int y, int x) {
    if (y < 0 || y >= N || x < 0 || x >= N || visited[y][x])
        return false;
    return true;
}
 
bool can_open(int y, int x) {
    for (int i = 0; i < 4; i++) {
        pair<intint> next = { y + dy[i], x + dx[i] };
        if (is_ok(next.first, next.second)) {
            int diff = abs(A[y][x] - A[next.first][next.second]);
            if (diff >= L && diff <= R)
                return true;
        }
    }
    return false;
}
 
void bfs(int y, int x) {
    queue <pair<intint>> q;
    q.push({ y,x });
    int Sum = 0;
    vector<pair<intint>> Union;
    while (!q.empty()) {
        pair<intint> now = { q.front().first, q.front().second };
        q.pop();
        if (visited[now.first][now.second])
            continue;
        visited[now.first][now.second] = true;
        Sum += A[now.first][now.second];
        Union.push_back(now);
        for (int i = 0; i < 4; i++) {
            pair<intint> next = { now.first + dy[i], now.second + dx[i] };
            if (is_ok(next.first, next.second)) {
                int diff = abs(A[now.first][now.second] - A[next.first][next.second]);
                if (diff >= L && diff <= R)
                    q.push(next);
            }
        }
    }
    int Value = Sum / Union.size();
 
    for (int i = 0; i < Union.size(); i++) {
        A[Union[i].first][Union[i].second] = Value;
    }
}
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> L >> R;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            cin >> A[i][j];
 
    bool chk = false;
    int ans = 0;
    while (!chk) {
        chk = true;
        memset(visited, falsesizeof(visited));
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (!visited[i][j] && can_open(i, j)) {
                    if (chk)
                        ans++;
                    bfs(i, j);
                    chk = false;
                }
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
cs

 

728x90