forked from encrypted-def/basic-algo-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16234.cpp
70 lines (61 loc) · 1.75 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
// Authored by : yongjunleeme
// Co-authored by : -
// http://boj.kr/7f59ea777614493ba11da2e4a572f9d9
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
int n, l, r;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int board[105][105];
int vis[105][105];
int ans = 0;
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> l >> r;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> board[i][j];
while(1){
bool flag = false;
for(int i = 0; i < 105; i++) fill(vis[i], vis[i]+105, 0);
queue<pair<int, int>> q;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(!vis[i][j]) q.push({i, j});
int sum = 0;
vector<pair<int, int>> tmp;
while(!q.empty()){
auto cur = q.front(); q.pop();
for(int dir = 0; dir < 4; dir++){
int nx = cur.X + dx[dir];
int ny = cur.Y + dy[dir];
if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
int diff = abs(board[cur.X][cur.Y] - board[nx][ny]);
if(diff >= l && diff <= r){
flag = 1;
if(!vis[cur.X][cur.Y]){
tmp.push_back({cur.X, cur.Y});
sum += board[cur.X][cur.Y];
vis[cur.X][cur.Y] = 1;
}
if(!vis[nx][ny]){
tmp.push_back({nx, ny});
sum += board[nx][ny];
vis[nx][ny] = 1;
q.push({nx,ny});
}
}
}
}
for(int k = 0; k < tmp.size(); k++)
board[tmp[k].X][tmp[k].Y] = sum/tmp.size();
}
}
if(!flag) break;
ans++;
}
cout << ans;
}