# LeetCode - 0892. Surface Area of 3D Shapes ### 題目網址:https://leetcode.com/problems/surface-area-of-3d-shapes/ ###### tags: `LeetCode` `Easy` `數學` `幾何` ```cpp= /* -LeetCode format- Problem: 892. Surface Area of 3D Shapes Difficulty: Easy by Inversionpeter */ class Solution { public: int surfaceArea(vector<vector<int>>& grid) { int area = 0, dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 }, newX, newY; for (int i = 0; i != grid.size(); ++i) for (int j = 0; j != grid[0].size(); ++j) if (grid[i][j]) { for (int k = 0; k < 4; ++k) { newX = j + dx[k]; newY = i + dy[k]; if (newX < 0 || newX == grid[0].size() || newY < 0 || newY == grid.size()) area += grid[i][j]; else if (grid[i][j] > grid[newY][newX]) area += grid[i][j] - grid[newY][newX]; } area += 2; } return area; } }; ```