Description Title
on N * N grid, we placed several cubes 1 * 1 * 1.
Each value v = grid [i] [j ] denotes the v cube superposed on the corresponding cell (i, j).
Please return to the final body surface area.
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
Solution: violent solution
In Example 2, for example,
First, a column to see a pillar, each pillar is composed of: two bottom surface (upper surface / lower surface) of the cube have contributed + all four side surface area
Then, after the column thoughtful together, we need to be bonded to lose surface area, the surface area of two columns that generally conform to the minimum of two high pillars 2 *
class Solution {
public:
int surfaceArea(vector<vector<int>>& grid) {
int n = grid.size(), area = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int hi = grid[i][j];
if(hi>0)
{
area += hi*4+2;
area -= i>0?min(hi, grid[i-1][j])*2:0;
area -= j>0?min(hi, grid[i][j-1]) *2:0;
}
}
}
return area;
}
};