목차

테트로미노

ps
링크acmicpc.net/…
출처BOJ
문제 번호14500
문제명테트로미노
레벨골드 5
분류

구현

시간복잡도O(nm)
인풋사이즈n<=500, m<=500
사용한 언어Python
제출기록35256KB / 1076ms
최고기록248ms
해결날짜2021/11/15

풀이

코드

코드 1 - 가지치기 없음

"""Solution code for "BOJ 14500. 테트로미노".

- Problem link: https://www.acmicpc.net/problem/14500
- Solution link: http://www.teferi.net/ps/problems/boj/14500

Tags: [Implementation]
"""

TETROMINOES = (
    # I
    ((0, 0), (0, 1), (0, 2), (0, 3)),
    ((0, 0), (1, 0), (2, 0), (3, 0)),
    # O
    ((0, 0), (0, 1), (1, 0), (1, 1)),
    # T
    ((0, 0), (0, 1), (0, 2), (1, 1)),
    ((0, 0), (1, 0), (1, 1), (2, 0)),
    ((0, 1), (1, 0), (1, 1), (1, 2)),
    ((0, 1), (1, 0), (1, 1), (2, 1)),
    # J
    ((0, 0), (0, 1), (0, 2), (1, 2)),
    ((0, 0), (0, 1), (1, 0), (2, 0)),
    ((0, 0), (1, 0), (1, 1), (1, 2)),
    ((0, 1), (1, 1), (2, 0), (2, 1)),
    # L
    ((0, 0), (0, 1), (0, 2), (1, 0)),
    ((0, 0), (1, 0), (2, 0), (2, 1)),
    ((0, 2), (1, 0), (1, 1), (1, 2)),
    ((0, 0), (0, 1), (1, 1), (2, 1)),
    # S
    ((0, 1), (0, 2), (1, 0), (1, 1)),
    ((0, 0), (1, 0), (1, 1), (2, 1)),
    # Z
    ((0, 0), (0, 1), (1, 1), (1, 2)),
    ((0, 1), (1, 0), (1, 1), (2, 0)),
)


def main():
    N, M = [int(x) for x in input().split()]
    grid = [[int(x) for x in input().split()] for _ in range(N)]

    answer = 0
    for tet in TETROMINOES:
        h = max(r for r, c in tet)
        w = max(c for r, c in tet)
        for r in range(N - h):
            for c in range(M - w):
                score = sum(grid[r + dr][c + dc] for dr, dc in tet)
                answer = max(score, answer)

    print(answer)


if __name__ == '__main__':
    main()

코드 2 - 가지치기

"""Solution code for "BOJ 14500. 테트로미노".

- Problem link: https://www.acmicpc.net/problem/14500
- Solution link: http://www.teferi.net/ps/problems/boj/14500

Tags: [Implementation]
"""

TETROMINOES = (
    # I
    ((0, 0), (0, 1), (0, 2), (0, 3)),
    ((0, 0), (1, 0), (2, 0), (3, 0)),
    # O
    ((0, 0), (0, 1), (1, 0), (1, 1)),
    # T
    ((0, 0), (0, 1), (0, 2), (1, 1)),
    ((0, 0), (1, 0), (1, 1), (2, 0)),
    ((0, 1), (1, 0), (1, 1), (1, 2)),
    ((0, 1), (1, 0), (1, 1), (2, 1)),
    # J
    ((0, 0), (0, 1), (0, 2), (1, 2)),
    ((0, 0), (0, 1), (1, 0), (2, 0)),
    ((0, 0), (1, 0), (1, 1), (1, 2)),
    ((0, 1), (1, 1), (2, 0), (2, 1)),
    # L
    ((0, 0), (0, 1), (0, 2), (1, 0)),
    ((0, 0), (1, 0), (2, 0), (2, 1)),
    ((0, 2), (1, 0), (1, 1), (1, 2)),
    ((0, 0), (0, 1), (1, 1), (2, 1)),
    # S
    ((0, 1), (0, 2), (1, 0), (1, 1)),
    ((0, 0), (1, 0), (1, 1), (2, 1)),
    # Z
    ((0, 0), (0, 1), (1, 1), (1, 2)),
    ((0, 1), (1, 0), (1, 1), (2, 0)),
)


def main():
    N, M = [int(x) for x in input().split()]
    grid = [[int(x) for x in input().split()] for _ in range(N)]

    max_num = max(max(row) for row in grid)
    possible_max_score = max_num * 4
    answer = 0
    for tet in TETROMINOES:
        h = max(r for r, c in tet)
        w = max(c for r, c in tet)
        for r in range(N - h):
            for c in range(M - w):
                score = possible_max_score
                for dr, dc in tet:
                    score += grid[r + dr][c + dc] - max_num
                    if score <= answer:
                        break
                else:
                    answer = score

    print(answer)


if __name__ == '__main__':
    main()