규도자 개발 블로그
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 11: 2D Arrays 본문
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 11: 2D Arrays
규도자 (gyudoza) 2019. 3. 22. 19:40Objective
Today, we're building on our knowledge of Arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video!
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 .
Sample Input
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample Output
19
Explanation
contains the following hourglasses:
1 1 1 1 1 0 1 0 0 0 0 0
1 0 0 0
1 1 1 1 1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0
1 1 0 0
0 0 2 0 2 4 2 4 4 4 4 0
1 1 1 1 1 0 1 0 0 0 0 0
0 2 4 4
0 0 0 0 0 2 0 2 0 2 0 0
0 0 2 0 2 4 2 4 4 4 4 0
0 0 2 0
0 0 1 0 1 2 1 2 4 2 4 0
The hourglass with the maximum sum () is:
2 4 4
2
1 2 4
풀이
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
arr = []
hourglass_sum_list = []
for _ in range(6):
arr.append(list(map(int, input().rstrip().split())))
for col_index in range(4):
for row_index in range(4):
hourglass_sum_list.append(arr[col_index][row_index] + arr[col_index][row_index + 1] + arr[col_index][row_index + 2] + arr[col_index + 1][row_index + 1] + arr[col_index + 2][row_index] + arr[col_index + 2][row_index + 1] + arr[col_index + 2][row_index + 2])
print(max(hourglass_sum_list))
설명
조금 공포스러운 모습이지만 저~~~~언혀 어려운 문제가 아니다. 6*6크기의 2차원배열이 입력되는데 모래시계모양으로 순차진행하면서 집합을 만들고 그렇게 만들어진 집합 중에서 요소를 전부 더한 값이 가장 컸을 때 몇이었는지 출력하는 것이다. 중간에 더하기를 한번에 하느라 코드가 좀 드럽게 됐다.
'알고리즘 > 풀이' 카테고리의 다른 글
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 13: Abstract Classes (0) | 2019.03.26 |
---|---|
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 12: Inheritance (0) | 2019.03.26 |
[백준/9012/파이썬(python3)] 괄호 (0) | 2019.03.22 |
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 10: Binary Numbers (0) | 2019.03.21 |
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 9: Recursion 3 (0) | 2019.03.21 |