> 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/892.surface_area_of_3d_shapes.md).

# 892.Surface Area of 3D Shapes

**892.Surface Area of 3D Shapes**

难度:Easy

> 在 N \* N 的网格上，我们放置一些 1 \* 1 \* 1 的立方体。

每个值 v = grid\[i]\[j] 表示 v 个正方体叠放在单元格 (i, j) 上。

返回最终形体的表面积。

```
示例 1：

输入：[[2]]
输出：10
示例 2：

输入：[[1,2],[3,4]]
输出：34
示例 3：

输入：[[1,0],[0,2]]
输出：16
示例 4：

输入：[[1,1,1],[1,0,1],[1,1,1]]
输出：32
示例 5：

输入：[[2,2,2],[2,1,2],[2,2,2]]
输出：46
 
```

提示：

1 <= N <= 50 0 <= grid\[i]\[j] <= 50

先计算总面积，然后计算相邻位置重叠的面，减去即可。\
单个位置的面积为`2*该位置上的立方体数量 +2`。

```
class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int res=0;
        int len=grid.size();
        for(int i=0;i<len;i++)
            for(int j=0;j<len;j++)
            {
                res= grid[i][j] ?res+ 4*grid[i][j]+2: res;
      //           cout<< res<<endl;
            }
       
            for(int i=0;i<len;i++)
            for(int j=0;j<len-1;j++)
            {
                res -=2*min(grid[i][j],grid[i][j+1]);
                
            }
            for(int i=0;i<len-1;i++)
            for(int j=0;j<len;j++)
            {
                res-=2*min(grid[i][j],grid[i+1][j]);
                
            }
        return res;
    }
};
```

> 执行用时 : 12 ms, 在Surface Area of 3D Shapes的C++提交中击败了96.30% 的用户\
> 内存消耗 : 9.1 MB, 在Surface Area of 3D Shapes的C++提交中击败了98.51% 的用户


---

# 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/892.surface_area_of_3d_shapes.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.
