just boring implementation problem
Intuition\n Describe your first thoughts on how to solve this problem. \nall you need to think about is how to implement what he wants as the constraints and th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nall you need to think about is how to implement what he wants as the constraints and the region size (3 * 3) helps brute force approach\n\n# Complexity\n- r -> number of rows, c -> number of columns\n- Time complexity: O( r * c (* 9) ) you can ignore 9 as it is just a constant time\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- Space complexity: O(r * c)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& g, int d) {\n        int r = int(g.size()), c = int(g[0].size());\n\n        int dx[] = { 1, -1, 0,  0 };\n        int dy[] = { 0,  0, 1, -1 };\n\n        vector<vector<int>> count(r, vector<int>(c));\n        vector<vector<int>> ans(r, vector<int>(c));\n        auto check_region = [&](int L_r, int R_r, int L_c, int R_c) {\n            if (R_r > r or R_c > c) {\n                return;\n            }\n            long long avg = 0;\n            for (int i = L_r; i < R_r; ++i) {\n                for (int j = L_c; j < R_c; ++j) {\n                    avg += g[i][j];\n                    auto is_valid = [&](int a, int b) {\n                        return a >= L_r and a < R_r and b >= L_c and b < R_c;\n                    };\n                    for (int k = 0, x, y; k < 4; ++k) {\n                        x = i + dx[k];\n                        y = j + dy[k];\n                        if (is_valid(x, y)) {\n                            if (abs(g[i][j] - g[x][y]) > d) {\n                                return;\n                            }\n                        }\n                    }\n                }\n            }\n            for (int i = L_r; i < R_r; ++i) {\n                for (int j = L_c; j < R_c; ++j) {\n                    ans[i][j] += avg / 9;\n                    ++count[i][j];\n                }\n            }\n            return;\n        };\n\n\n        for (int i = 0; i < r; ++i) {\n            for (int j = 0; j < c; ++j) {\n                check_region(i, i + 3, j, j + 3);\n            }\n        }\n\n        for (int i = 0; i < r; ++i) {\n            for (int j = 0; j < c; ++j) {\n                if (not count[i][j]) {\n                    ans[i][j] = g[i][j];\n                } else {\n                    ans[i][j] /= count[i][j];\n                }\n            }\n        }\n\n        return ans;\n    }\n};\n```
['C++']