-
Notifications
You must be signed in to change notification settings - Fork 0
/
16234.cpp
79 lines (64 loc) · 1.82 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
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
vector<vector<int> > A;
vector<vector<int> > visited;
vector<pair<int, int> > lst;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int N, L, R;
int sum = 0;
int cnt = 0;
void func(int x, int y) {
visited[x][y] = 1;
sum += A[x][y];
cnt++;
lst.push_back(make_pair(x, y));
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && nx < N && ny >= 0 && ny < N && visited[nx][ny] == 0) {
int diff = abs(A[x][y] - A[nx][ny]);
if(diff >= L && diff <= R) {
func(nx, ny);
}
}
}
}
int main() {
cin >> N >> L >> R;
A.resize(N, vector<int>(N));
visited.resize(N, vector<int>(N));
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
cin >> A[i][j];
}
}
int days = 0;
while(true) {
bool movement = false;
visited.assign(N, vector<int>(N, 0));
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(visited[i][j] == 0) {
sum = 0;
cnt = 0;
lst.clear();
func(i, j);
if(cnt > 1) { // cnt 자기 빼고 2이상
movement = true;
int newPopulation = sum / cnt;
for(int k=0; k<lst.size(); k++) {
A[lst[k].first][lst[k].second] = newPopulation;
}
}
}
}
}
if(!movement) break; //cnt가 1 or 0일때
days++;
}
cout << days << endl;
return 0;
}