-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16234.cpp
96 lines (79 loc) · 1.58 KB
/
16234.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
#include <cstring>
using namespace std;
int N, L, R;
int map[51][51];
int visited[51][51];
int val[2501];
int dy[4] = { 0,-1,0,1 };
int dx[4] = { 1,0,-1,0 };
int ans = 0;
struct node {
int y, x;
};
void bfs(int y, int x, int ct) {
queue<node> que;
que.push({ y,x });
visited[y][x] = ct;
int sum = 0;
int many = 0;
while (!que.empty()) {
int qy = que.front().y;
int qx = que.front().x;
sum += map[qy][qx];
many++;
que.pop();
for (int i = 0; i < 4; i++) {
int ny = qy + dy[i];
int nx = qx + dx[i];
int gap = abs(map[ny][nx] - map[qy][qx]);
if (ny >= 0 && nx >= 0 && ny < N && nx < N) {
if (!visited[ny][nx]) {
if (L <= gap && gap <= R) {
visited[ny][nx] = ct;
que.push({ ny,nx });
}
}
}
}
}
val[ct] = sum / many;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> L >> R;
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
cin >> map[y][x];
}
}
while (1) {
memset(visited, 0, sizeof(visited));
int count = 0;
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
if (visited[y][x] == 0) {
count++;
bfs(y, x, count);
}
}
}
//debug_print();
if (count == N * N) break;
ans++;
for (int i = 1; i <= count; i++) {
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
if (visited[y][x] == i) map[y][x] = val[i];
}
}
}
}
cout << ans;
return 0;
}