Force buckle three-dimensional body surface area of 892 title

Force buckle three-dimensional body surface area of ​​892 title

class Solution {
    public:
    int surfaceArea(vector<vector<int>>& grid) 
    {
        int lenr = grid.size();
        if (lenr == 0)
            return 0;
        int lenc = grid[0].size();
        int dr[4] = { 0, 0, 1, -1 };
        int dc[4] = { -1, 1, 0, 0 };
        int result = 0;
        int r, c;
        for (int i = 0; i < lenr; i++)
        {
            for (int j = 0; j < lenc; j++)
            {
                if (grid[i][j] <= 0)
                    continue;
                result += 2; // 上下两个面
                for (int k = 0; k < 4; k++)
                {
                    r = i + dr[k];
                    c = j + dc[k];
                    

                    if (r < 0 || r >= lenr || c < 0 || c >= lenc)
                        result += grid[i][j];
                    else
                        result += max<int>(grid[i][j] - grid[r][c], 0); // 相邻的可能比当前的高,此时是0
                }
            }
        }
        return result;
    }

};

Guess you like

Origin www.cnblogs.com/woodjay/p/12571197.html