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. v.push_back({ny, nx});
  16. dfs(ny, nx, v);
  17. }
  18. }
  19. }
  20.  
  21. int main(){
  22. cin >> n >> r >> l;
  23. for(int i = 0; i < n; i++){
  24. for(int j = 0; j < n; j++){
  25. cin >> a[i][j];
  26. }
  27. }
  28.  
  29. while(true){
  30. fill(&visited[0][0], &visited[0][0] + 51 * 51, 0);
  31. bool isEnd = true;
  32.  
  33. for(int i = 0; i < n; i++){
  34. for(int j = 0; j < n; j++){
  35. if(!visited[i][j]){
  36. v.clear();
  37. visited[i][j] = 1;
  38. v.push_back({i, j});
  39. sum = a[i][j];
  40. dfs(i, j, v);
  41.  
  42. if(v.size() == 1) continue;
  43.  
  44. for(auto b : v){
  45. a[b.first][b.second] = sum / v.size();
  46. isEnd = false;
  47. }
  48. }
  49. }
  50. }
  51. if(isEnd) break;
  52. cnt++;
  53. }
  54. cout << cnt << '\n';
  55. }
Success #stdin #stdout 0s 5320KB
stdin
2 20 50
50 30
30 40
stdout
0