-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmain.cpp
75 lines (66 loc) · 1.69 KB
/
main.cpp
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
// Authored by : tony9402
// Co-authored by : -
// Link : http://boj.kr/c8a89727dad74664813faf3ff7a4dd9c
#include<bits/stdc++.h>
using namespace std;
queue<pair<int, int>> Q;
const int dy[] = {-1, 1, 0, 0};
const int dx[] = { 0, 0,-1, 1};
int Map[55][55], N, L, R;
bool used[55][55];
void BFS(int a, int b) {
if(used[a][b]) return;
int total = 0, cnt = 0;
vector<pair<int, int>> path;
queue <pair<int, int>> q;
q.emplace(a, b);
used[a][b] = true;
while(!q.empty()) {
auto [y, x] = q.front(); q.pop();
total += Map[y][x];
cnt ++;
path.emplace_back(y, x);
for(int k=0;k<4;k++) {
int qy = y + dy[k];
int qx = x + dx[k];
if(0 > qy || qy >= N || 0 > qx || qx >= N)continue;
if(used[qy][qx]) continue;
int value = abs(Map[qy][qx] - Map[y][x]);
if(L > value || value > R) continue;
used[qy][qx] = true;
q.emplace(qy, qx);
}
}
if(cnt < 2) return;
for(auto &[y, x]: path) {
Map[y][x] = total / cnt;
Q.emplace(y, x);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> L >> R;
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
cin >> Map[i][j];
Q.emplace(i, j);
}
}
int answer = -1;
while(!Q.empty()) {
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
used[i][j] = false;
}
}
int qsize = Q.size();
answer ++;
for(int i = 0; i < qsize; i++) {
auto [y, x] = Q.front(); Q.pop();
BFS(y, x);
}
}
cout << answer;
return 0;
}