fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, r, l, visited[51][51], a[51][51], cnt, sum;
  4. vector<pair<int, int>> v;
  5. int dy[] = {-1, 0, 1, 0};
  6. int dx[] = {0, 1, 0, -1};
  7.  
  8. void dfs(int y, int x, vector<pair<int, int>> &v){
  9. for(int i = 0; i < 4; i++){
  10. int ny = y + dy[i];
  11. int nx = x + dx[i];
  12. if(ny < 0 || ny >= n || nx < 0 || nx >= n || visited[ny][nx]) continue;
  13. if(abs(a[y][x] - a[ny][nx]) >= l && abs(a[y][x] - a[ny][nx]) <= r){
  14. sum += a[ny][nx];
  15. visited[ny][nx] = 1;
  16. v.push_back({ny, nx});
  17. dfs(ny, nx, v);
  18. }
  19. }
  20. }
  21.  
  22. int main(){
  23. cin >> n >> r >> l;
  24. for(int i = 0; i < n; i++){
  25. for(int j = 0; j < n; j++){
  26. cin >> a[i][j];
  27. }
  28. }
  29.  
  30. while(true){
  31. fill(&visited[0][0], &visited[0][0] + 51 * 51, 0);
  32. bool isEnd = true;
  33.  
  34. for(int i = 0; i < n; i++){
  35. for(int j = 0; j < n; j++){
  36. if(!visited[i][j]){
  37. v.clear();
  38. visited[i][j] = 1;
  39. v.push_back({i, j});
  40. sum = a[i][j];
  41. dfs(i, j, v);
  42.  
  43. if(v.size() == 1) continue;
  44.  
  45. for(auto b : v){
  46. a[b.first][b.second] = sum / v.size();
  47. isEnd = false;
  48. }
  49. }
  50. }
  51. }
  52. if(isEnd) break;
  53. cnt++;
  54. }
  55. cout << cnt << '\n';
  56. }
Success #stdin #stdout 0.01s 5320KB
stdin
3 5 10
10 15 20
20 30 25
40 22 10
stdout
0