# 一、中文版

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.

Example 1:

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

class Solution:
def surfaceArea(self, grid: List[List[int]]) -> int:
ans = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] > 0:
ans += 2
for nr,nc in ((i+1,j),(i-1,j),(i,j+1),(i,j-1)):
if 0 <= nr < len(grid) and 0 <= nc < len(grid[i]):
nval = grid[nr][nc]
else:
nval = 0
ans += max(grid[i][j] - nval,0)
return ans

# 四、解题报告

1、任何一个小方格都会有上下两个面漏在外面，所以要 ans += 2；

2、此外，会存在相邻两个小方格挡住对方表面的情况。所以此时对每个小方格判断他的上下左右四个方向是否存在相邻小方格，若存在，记录其高度（也就是 grid[nr][nc]）。对于小方格 grid[i][j] 来说就判断它比四周相邻小方格高出多少，就知道它漏在外面的表面积是多少，所以用 grid[i][j] - nval，如果它比它旁边的矮，说明没有表面漏在这一侧，ans += 0.

https://leetcode-cn.com/problems/surface-area-of-3d-shapes/solution/san-wei-xing-ti-de-biao-mian-ji-by-leetcode-soluti/

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客