> 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% 的用户
