> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/661.image_smoother.md).

# 661.Image Smoother

**661.Image Smoother**

难度:Easy

> 包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ，平均灰度的计算是周围的8个单元和它本身的值求平均，如果周围的单元格不足八个，则尽可能多的利用它们。

```
示例 1:

输入:
[[1,1,1],
 [1,0,1],
 [1,1,1]]
输出:
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]
解释:
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
注意:

给定矩阵中的整数范围为 [0, 255]。
矩阵的长和宽的范围均为 [1, 150]。
```

注意一下特殊情况就行，其他按题意平滑。

```
class Solution {
public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
        int r=M.size();
        int c=M[0].size();
        if(r==1 && c==1) return M;
        vector<vector<int>> res(M);
        if(r==1)
        {
            res[0][0]=(M[0][0]+M[0][1])/2;
            res[0][c-1]=(M[0][c-1]+M[0][c-2])/2;
            for(int i=1;i<c-1;i++)
            res[0][i] = (M[0][i] + M[0][i-1] +M[0][i+1] )/3;
            return res;
        }
       if(c==1)
        {
            res[0][0]=(M[0][0]+M[1][0])/2;
            res[r-1][0]=(M[r-1][0]+M[r-2][0])/2;
            for(int i=1;i<r-1;i++)
            res[i][0] = (M[i][0] + M[i-1][0] +M[i+1][0])/3;
            return res;
        }

        for(int i=1;i<r-1;i++)
            for(int j=1;j<c-1;j++)
                res[i][j] =(M[i][j] +M[i-1][j]+ M[i][j-1] + M[i-1][j-1]+ M[i][j+1] + M[i+1][j] + M[i+1][j+1] + M[i-1][j+1] + M[i+1][j-1])/9;
        for(int i=1;i<c-1;i++)
            res[0][i] = (M[0][i] + M[0][i-1] +M[0][i+1] +M[1][i] + M[1][i-1]+M[1][i+1])/6;
        for(int i=1;i<c-1;i++)
             res[r-1][i] = (M[r-1][i] + M[r-1][i-1] +M[r-1][i+1] +M[r-2][i] + M[r-2][i-1]+M[r-2][i+1])/6;
        for(int i=1;i<r-1;i++)
            res[i][0] = (M[i][0] + M[i-1][0] +M[i+1][0] +M[i][1] + M[i-1][1]+M[i+1][1])/6;
        
        for(int i=1;i<r-1;i++)
                res[i][c-1] = (M[i][c-1] + M[i-1][c-1] +M[i+1][c-1] +M[i][c-2] + M[i-1][c-2]+M[i+1][c-2])/6;
        
        res[0][0]=(M[0][0]+M[0][1] +M[1][1]+M[1][0])/4;
        res[0][c-1]=(M[0][c-1]+M[0][c-2]+M[1][c-1]+M[1][c-2])/4;
        res[r-1][0]=(M[r-1][0]+M[r-1][1]+M[r-2][0]+M[r-2][1])/4;
        res[r-1][c-1]=(M[r-1][c-1]+M[r-2][c-1]+M[r-2][c-2]+M[r-1][c-2])/4;
        return res;
    }
        
};
```

> 执行用时 :328ms, 在所有 C++ 提交中击败了37.50%的用户\
> 内存消耗 :17.5MB, 在所有 C++ 提交中击败了86.36%的用户


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dfine.gitbook.io/leetcode/661.image_smoother.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
