-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(Implementation)3D_Surface_Area.py
52 lines (39 loc) · 1.14 KB
/
(Implementation)3D_Surface_Area.py
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
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the surfaceArea function below.
def surfaceArea(A, H, W):
to_reduce = []
total = 6 * (sum([sum(row) for row in A]))
for i in range(H):
for j in range(W):
left, right, up, down = 0, 0, 0, 0
#left
if 0 <= j - 1 < W:
left = min(A[i][j], A[i][j - 1])
#right
if 0 <= j + 1 < W:
right = min(A[i][j], A[i][j + 1])
#up
if 0 <= i - 1 < H:
up = min(A[i][j], A[i - 1][j])
#down
if 0 <= i + 1 < H:
down = min(A[i][j], A[i + 1][j])
to_reduce.append(left + right + up + down)
to_reduce.append((A[i][j] - 1) * 2)
return (total - sum(to_reduce))
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
HW = input().split()
H = int(HW[0])
W = int(HW[1])
A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A, H, W)
fptr.write(str(result) + '\n')
fptr.close()