Surface Area of 3D Shapes

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

 

// 上下面积都是 size*size 关键是侧面积
// 遍历每个点 统计四个方向上的侧面积(如果周围没有则直接加上这个高度)

// 另一种思路 算上所有的  减去多算的 (值考虑左和上)
class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int w=grid[0].size(), h=grid.size();
        vector<pair<int, int>> walk={{1,0},{-1,0},{0,1}, {0,-1}};
        int area=0, cnt=0;
        for(int j=0;j<h;j++){
            for(int i=0;i<w;i++){
                for(int k=0;k<4;k++){
                    if(grid[j][i] != 0) cnt++;
                    int tmpj = j+walk[k].first, tmpi= i+walk[k].second;
                    if(tmpj==h || tmpj<0 || tmpi==w || tmpi<0)  area += grid[j][i];
                    else if(grid[j][i] > grid[tmpj][tmpi])  area += grid[j][i] - grid[tmpj][tmpi];
                }
            }
        }
        area += cnt*2;
        return area;
    }
};
lass Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int res = 0, n = grid.size();
        for (int i = 0; i < n; ++i) 
        {
            for (int j = 0; j < n; ++j) 
            {
                // get all surface 
                if (grid[i][j]) 
                    res += grid[i][j] * 4 + 2;
                // remove above and left attaching surface
                if (i) 
                    res -= min(grid[i][j], grid[i - 1][j]) * 2;
                if (j) 
                    res -= min(grid[i][j], grid[i][j - 1]) * 2;
            }
        }
        return res;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个可能的实现,其中球、圆柱和圆锥都是基于一个名为“Shape”的基类派生的: ```cpp #include <iostream> #include <cmath> using namespace std; class Shape { protected: double radius; public: Shape(double r = 0) : radius(r) {} virtual double getArea() const = 0; virtual double getVolume() const = 0; virtual void print() const = 0; }; class Sphere : public Shape { public: Sphere(double r) : Shape(r) {} double getArea() const { return 4 * M_PI * radius * radius; } double getVolume() const { return 4.0 / 3.0 * M_PI * radius * radius * radius; } void print() const { cout << "Sphere with radius " << radius << ":" << endl; cout << "Surface area = " << getArea() << endl; cout << "Volume = " << getVolume() << endl; } }; class Cylinder : public Shape { private: double height; public: Cylinder(double r, double h) : Shape(r), height(h) {} double getArea() const { return 2 * M_PI * radius * height + 2 * M_PI * radius * radius; } double getVolume() const { return M_PI * radius * radius * height; } void print() const { cout << "Cylinder with radius " << radius << " and height " << height << ":" << endl; cout << "Surface area = " << getArea() << endl; cout << "Volume = " << getVolume() << endl; } }; class Cone : public Shape { private: double height; public: Cone(double r, double h) : Shape(r), height(h) {} double getArea() const { return M_PI * radius * (radius + sqrt(height*height + radius*radius)); } double getVolume() const { return 1.0 / 3.0 * M_PI * radius * radius * height; } void print() const { cout << "Cone with radius " << radius << " and height " << height << ":" << endl; cout << "Surface area = " << getArea() << endl; cout << "Volume = " << getVolume() << endl; } }; int main() { Shape* shapes[3]; shapes[0] = new Sphere(2); shapes[1] = new Cylinder(2, 4); shapes[2] = new Cone(2, 4); for (int i = 0; i < 3; i++) { shapes[i]->print(); cout << endl; } return 0; } ``` 这个程序的输出将是: ``` Sphere with radius 2: Surface area = 50.2655 Volume = 33.5103 Cylinder with radius 2 and height 4: Surface area = 75.3982 Volume = 50.2655 Cone with radius 2 and height 4: Surface area = 40.8407 Volume = 16.7552 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值