# C#LeetCode刷题之#892-三维形体的表面积（Surface Area of 3D Shapes）

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

On a N * N grid, we place some 1 * 1 * 1 cubes.

Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).

Return the total surface area of the resulting shapes.

Input: [[2]]
Output: 10
Example 2:

Input: [[1,2],[3,4]]
Output: 34
Example 3:

Input: [[1,0],[0,2]]
Output: 16
Example 4:

Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 32
Example 5:

Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 46

Note:

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

```public class Program {

public static void Main(string[] args) {
var grid = new int[][] { new int[] { 2 } };

var res = SurfaceArea(grid);

Console.WriteLine(res);

}

public static int SurfaceArea(int[][] grid) {
var res = 0;
for(var i = 0; i < grid.Length; i++)
for(var j = 0; j < grid[0].Length; j++) {
if(grid[i][j] > 0)
//若有立方体，总的表面积为立方体的 4 个边加上下底
res += 4 * grid[i][j] + 2;
if(i < grid.Length - 1)
//若在 x 轴上，不是 x 轴上的最后一个
//那么减去它们重叠的部分 * 2
res -= Math.Min(grid[i][j], grid[i + 1][j]) * 2;
if(j < grid[0].Length - 1)
//若在 y 轴上，不是 y 轴上的最后一个
//那么减去它们重叠的部分 * 2
res -= Math.Min(grid[i][j], grid[i][j + 1]) * 2;
}
return res;
}

}```

`10`

(11)