๋ฌธ์ ๋งํฌ
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<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> 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<int, int> 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<int, int>> q;
q.push({ y,x });
int Sum = 0;
vector<pair<int, int>> Union;
while (!q.empty()) {
pair<int, int> 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<int, int> 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, false, sizeof(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 |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 17144 : ๋ฏธ์ธ๋จผ์ง ์๋ ! (0) | 2022.05.11 |
---|---|
[BOJ] 16235 : ๋๋ฌด ์ฌํ ํฌ (0) | 2022.05.11 |
[BOJ] 15686 : ์นํจ ๋ฐฐ๋ฌ (0) | 2022.05.09 |
[BOJ] 15685 : ๋๋๊ณค ์ปค๋ธ (0) | 2022.05.09 |
[BOJ] 9879 : Cross Country Skiing (1) | 2022.05.03 |