728x90
Overview
- 체감 난이도: ★★☆☆☆
- 문제 레벨: IL / Simulation / 격자 안에서 완전 탐색 / 연습 문제
- 문제 유형: Simulation
- 풀이 상태: 답안참고 / 스스로 해결
- 추후: 다시 풀어보기 / 간단 복습 / 완벽 이해
[문제]
Trail 4 / Chapter 1 / Lesson 1 / 연습 문제
https://www.codetree.ai/trails/complete/curated-cards/challenge-tromino/description
Code Tree | Learning to Code with Confidence
A super-comprehensive, meticulously arranged Coding Learning Curriculum engineered by Algorithm Experts composed of former International Olympiad in Informatics (IOI) medalists.
www.codetree.ai
[코드]
풀이 방법: ㄱ자 블록과 일자 블록의 경우를 나누어서 구했다.
n, m = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(n)]
# Write your code here!
def in_range(dr):
x, y = dr
return 0 <= x < n and 0 <= y < m
def block1(x, y):
# x, y 기준으로 (위, 오른쪽), (오른쪽, 아래), (아래, 왼쪽), (왼쪽, 위) 비교
arr = []
# 좌, 상, 우, 하
left, up, right, down = (x, y - 1), (x - 1, y), (x, y + 1), (x + 1, y)
mx, num = 0, grid[x][y]
# 범위 확인
if in_range(left) and in_range(up):
lx, ly = left
ux, uy = up
mx = max(mx, num + grid[lx][ly] + grid[ux][uy])
if in_range(up) and in_range(right):
rx, ry = right
ux, uy = up
mx = max(mx, num + grid[rx][ry] + grid[ux][uy])
if in_range(right) and in_range(down):
rx, ry = right
dx, dy = down
mx = max(mx, num + grid[rx][ry] + grid[dx][dy])
if in_range(down) and in_range(left):
lx, ly = left
dx, dy = down
mx = max(mx, num + grid[lx][ly] + grid[dx][dy])
return mx
def block2(x, y):
# x, y 기준으로 세로로 3칸, 가로로 3칸 더해서 최댓값 비교
col = 0
if x + 3 <= n:
for i in range(x, x + 3):
col += grid[i][y]
# 가로
row = 0
if y + 3 <= m:
for i in range(y, y + 3):
row += grid[x][i]
return max(row, col)
answer = 0
for i in range(n):
for j in range(m):
b1 = block1(i, j)
b2 = block2(i, j)
answer = max(answer, b1, b2)
print(answer)
일일히 if 문으로 경우를 쪼개주는 대신 일반화 한다면,
n, m = map(int, input().split())
# 격자 입력받기
grid = []
for _ in range(n):
row = list(map(int, input().split()))
grid.append(row)
def in_range(x, y):
return 0 <= x < n and 0 <= y < m
def get_straight_sum(x, y, length, is_vertical):
if is_vertical and x + length > n:
return 0
if not is_vertical and y + length > m:
return 0
total = 0
for i in range(length):
nx = x + (i if is_vertical else 0)
ny = y + (0 if is_vertical else i)
total += grid[nx][ny]
return total
def get_corner_sum(x, y, directions):
# 범위 체크
for dx, dy in directions:
if not in_range(x + dx, y + dy):
return 0
# 중심점
total = grid[x][y]
# 방향 합 더하기
for dx, dy in directions:
total += grid[x + dx][y + dy]
return total
def get_max_sum(x, y):
# 직선 모양 (가로, 세로)
straight_sum = max(
get_straight_sum(x, y, 3, True), # 세로
get_straight_sum(x, y, 3, False) # 가로
)
# 코너 모양
corner_patterns = [
[(-1,0), (0,-1)], # 좌상
[(-1,0), (0,1)], # 우상
[(1,0), (0,1)], # 우하
[(1,0), (0,-1)] # 좌하
]
# 모든 코너 패턴에 대해 최댓값 구하기
corner_max = 0
for pattern in corner_patterns:
corner_max = max(corner_max, get_corner_sum(x, y, pattern))
return max(straight_sum, corner_max)
# 모든 위치에서 최댓값 구하기
answer = 0
for i in range(n):
for j in range(m):
answer = max(answer, get_max_sum(i, j))
print(answer)
반응형
'🚩 Coding Test > Code Tree' 카테고리의 다른 글
[Code Tree][Python] IL Simulation 1차원 바람 (0) | 2025.02.12 |
---|---|
[Code Tree][Python] IL Simulation 금 채굴하기 (0) | 2025.01.24 |
[Code Tree][Python] 최대로 겹치는 지점 + 회고 (1) | 2024.11.09 |
[Code Tree][Python] BFS / 돌 잘 치우기 (0) | 2024.11.08 |
[Code Tree][Python] BFS / K번 최댓값으로 이동하기 (1) | 2024.11.08 |