forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
54 lines (44 loc) · 862 Bytes
/
solution.cpp
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
49
50
51
52
53
54
#include <iostream>
using namespace std;
int mod(int a);
int main()
{
const int MAX_SIZE = 102;
int rows, cols,price,i,j;
int mat[MAX_SIZE][MAX_SIZE];
cin >> rows >> cols;
//we surround the matrix with 0s
for (i = 0; i < rows + 2; i++)
{
mat[i][0] = 0;
mat[i][cols+1] = 0;
}
for (i = 0; i < cols + 2; i++)
{
mat[0][i] = 0;
mat[rows+1][i] = 0;
}
//We read the values
for (i = 1; i <= rows; i++)
for (j = 1; j <= cols; j++)
cin >> mat[i][j];
//We compute up and down views.
price = (rows * cols)* 2;
//We compute from left to right view and from behind to infront view
for (int i = 0; i < rows+1; i++)
{
for (int j = 0; j < cols+1; j++)
{
price += mod(mat[i][j] - mat[i][j+1]);
price += mod(mat[i][j] - mat[i+1][j]);
}
}
cout << price << endl;
return 0;
}
int mod(int a)
{
if (a < 0)
a = -a;
return a;
}