- 문제
- 2차원 배열이 주어진다. 이 때 이웃한 네개의 영역의 합의 최댓값을 구하시오.
- 이웃한다란, 위, 아래, 좌, 우로 이동할 수 있음을 가리킨다.
- 해결
import sys
height, width = [int(x) for x in sys.stdin.readline().split()]
mapping = [list(map(int, x.split())) for x in sys.stdin.readlines()]
case_to_sub = {
0: 2,
1: 1,
2: 8,
3: 4,
4: 4
}
def case_sub_to_available(case, sub, total_row, total_col):
if case == 0 and sub == 0:
return total_row, total_col - 3, 0, 0
elif case == 0 and sub == 1:
return total_row - 3, total_col, 0, 0
elif case == 1 and sub == 0:
return total_row - 1, total_col - 1, 0, 0
elif case == 2 and sub == 0:
return total_row - 2, total_col - 1, 0, 0
elif case == 2 and sub == 1:
return total_row - 1, total_col - 2, 0, 0
elif case == 2 and sub == 2:
return total_row - 2, total_col - 1, 0, 0
elif case == 2 and sub == 3:
return total_row - 1, total_col - 2, 1, 0
elif case == 2 and sub == 4:
return total_row - 2, total_col - 1, 2, 0
elif case == 2 and sub == 5:
return total_row - 1, total_col - 2, 0, 0
elif case == 2 and sub == 6:
return total_row - 2, total_col - 1, 0, 0
elif case == 2 and sub == 7:
return total_row - 1, total_col - 2, 0, 0
elif case == 3 and sub == 0:
return total_row - 2, total_col - 1, 0, 0
elif case == 3 and sub == 1:
return total_row - 1, total_col - 2, 1, 0
elif case == 3 and sub == 2:
return total_row - 2, total_col - 1, 1, 0
elif case == 3 and sub == 3:
return total_row - 1, total_col - 2, 0, 0
elif case == 4 and sub == 0:
return total_row - 1, total_col - 2, 0, 0
elif case == 4 and sub == 1:
return total_row - 2, total_col - 1, 1, 0
elif case == 4 and sub == 2:
return total_row - 1, total_col - 2, 1, 0
elif case == 4 and sub == 3:
return total_row - 2, total_col - 1, 0, 0
def get_val(case, sub, r, c, d):
if case == 0 and sub == 0:
return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r][c + 3]
elif case == 0 and sub == 1:
return d[r][c] + d[r + 1][c] + d[r + 2][c] + d[r + 3][c]
elif case == 1 and sub == 0:
return d[r][c] + d[r + 1][c] + d[r][c + 1] + d[r + 1][c + 1]
elif case == 2 and sub == 0:
return d[r][c] + d[r + 1][c] + d[r + 2][c] + d[r + 2][c + 1]
elif case == 2 and sub == 1:
return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r + 1][c]
elif case == 2 and sub == 2:
return d[r][c] + d[r][c + 1] + d[r + 1][c + 1] + d[r + 2][c + 1]
elif case == 2 and sub == 3:
return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r - 1][c + 2]
elif case == 2 and sub == 4:
return d[r][c] + d[r][c + 1] + d[r - 1][c + 1] + d[r - 2][c + 1]
elif case == 2 and sub == 5:
return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r + 1][c + 2]
elif case == 2 and sub == 6:
return d[r][c] + d[r][c + 1] + d[r + 1][c] + d[r + 2][c]
elif case == 2 and sub == 7:
return d[r][c] + d[r + 1][c] + d[r + 1][c + 1] + d[r + 1][c + 2]
elif case == 3 and sub == 0:
return d[r][c] + d[r + 1][c] + d[r + 1][c + 1] + d[r + 2][c + 1]
elif case == 3 and sub == 1:
return d[r][c] + d[r][c + 1] + d[r - 1][c + 1] + d[r - 1][c + 2]
elif case == 3 and sub == 2:
return d[r][c] + d[r][c + 1] + d[r - 1][c + 1] + d[r + 1][c]
elif case == 3 and sub == 3:
return d[r][c] + d[r][c + 1] + d[r + 1][c + 1] + d[r + 1][c + 2]
elif case == 4 and sub == 0:
return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r + 1][c + 1]
elif case == 4 and sub == 1:
return d[r][c] + d[r][c + 1] + d[r - 1][c + 1] + d[r + 1][c + 1]
elif case == 4 and sub == 2:
return d[r][c] + d[r][c + 1] + d[r][c + 2] + d[r - 1][c + 1]
elif case == 4 and sub == 3:
return d[r][c] + d[r + 1][c] + d[r + 1][c + 1] + d[r + 2][c]
max_ = 0
for case in case_to_sub.keys():
for sub in range(case_to_sub[case]):
row_cnt, col_cnt, start_row, start_col = case_sub_to_available(case, sub, height, width)
for r in range(start_row, start_row + row_cnt, 1):
for c in range(start_col, start_col + col_cnt, 1):
val = get_val(case, sub, r, c, mapping)
if val > max_:
max_ = val
print(max_)
- 포인트
- 테트로미노와 같다.
- Bruteforce로 해결한다.