11: 2D Arrays(python)
2D Arrays(python)
Context
Given a 2D Array, :
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in to be a subset of values with indices falling in this pattern in 's graphical representation:
a b c
d
e f g
There are hourglasses in , and an hourglass sum is the sum of an hourglass' values.
Task
Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.
Input Format
There are lines of input, where each line contains space-separated integers describing 2D Array ; every value in will be in the inclusive range of to .
Constraints
Output Format
Print the largest (maximum) hourglass sum found in .
Solution :
arr = []
for i in range(6):
arr_t = [int(arr_temp) for arr_temp in input().strip().split(' ')]
arr.append(arr_t)
sm = -9 * 7
for row in range(len(arr) - 2):
for column in range(len(arr[row]) - 2):
tl = arr[row][column]
tc = arr[row][column + 1]
tr = arr[row][column + 2]
mc = arr[row + 1][column + 1]
bl = arr[row + 2][column]
bc = arr[row + 2][column + 1]
br = arr[row + 2][column + 2]
s = tl + tc + tr + mc + bl + bc + br
sm = max(s, sm)
print(sm)
Comments
Post a Comment