-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsurface_area.cpp
More file actions
48 lines (44 loc) · 1.48 KB
/
surface_area.cpp
File metadata and controls
48 lines (44 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Madison, is a little girl who is fond of toys.
* Her friend Mason works in a toy manufacturing factory.
* Mason has a 2D board A of size H x W with H rows and W columns.
* The board is divided into cells of size 1 x 1 with each cell indicated
* by it's coordinate (i, j). The cell (i, j) has an integer A_ij written on it.
* To create the toy Mason stacks A_ij number of cubes of size 1 x 1 x 1 on the
* cell (i, j). Given the description of the board showing the values of A_ij
* and that the price of the toy is equal to the 3D surface area find the price
* of the toy.
*/
#include "surface_area.hpp"
#include <cstdlib>
using namespace std;
long long surface_area(const std::vector<std::vector<int>>& figure) {
if (figure.empty() || figure[0].empty()) {
return 0;
}
size_t n = figure.size(), m = figure[0].size();
long long area = 2 * n * m;
for (size_t i = 0; i < n; ++i) {
area += figure[i][0];
}
for (size_t i = 0; i < n; ++i) {
area += figure[i][m - 1];
}
for (size_t j = 0; j < m; ++j) {
area += figure[0][j];
}
for (size_t j = 0; j < m; ++j) {
area += figure[n - 1][j];
}
for (size_t i = 0; i < n; ++i) {
for (size_t j = 1; j < m; ++j) {
area += abs(figure[i][j] - figure[i][j - 1]);
}
}
for (size_t j = 0; j < m; ++j) {
for (size_t i = 1; i < n; ++i) {
area += abs(figure[i][j] - figure[i - 1][j]);
}
}
return area;
}