(삼성) - 테트로미노(14500)

24820 단어 백준삼성백준

삼성 역량테스트 - 구현, 브루트포스

import sys

input = sys.stdin.readline
n, m = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(n)]
answer = 0
tetromino = [
    [(0,0), (0,1), (1,0), (1,1)], 
    [(0,0), (0,1), (0,2), (0,3)], 
    [(0,0), (1,0), (2,0), (3,0)], 
    [(0,0), (0,1), (0,2), (1,0)], 
    [(1,0), (1,1), (1,2), (0,2)],
    [(0,0), (1,0), (1,1), (1,2)], 
    [(0,0), (0,1), (0,2), (1,2)], 
    [(0,0), (1,0), (2,0), (2,1)],
    [(2,0), (2,1), (1,1), (0,1)],
    [(0,0), (0,1), (1,0), (2,0)], 
    [(0,0), (0,1), (1,1), (2,1)],
    [(0,0), (0,1), (0,2), (1,1)], 
    [(1,0), (1,1), (1,2), (0,1)], 
    [(0,0), (1,0), (2,0), (1,1)], 
    [(1,0), (0,1), (1,1), (2,1)], 
    [(1,0), (2,0), (0,1), (1,1)],
    [(0,0), (1,0), (1,1), (2,1)],
    [(1,0), (0,1), (1,1), (0,2)],
    [(0,0), (0,1), (1,1), (1,2)]
]

def find(x, y):
    global answer
    for i in range(19):
        result = 0 # 각 테트로미노 4개 값 누적
        for j in range(4):
            # try, except 예외처리 -> 범위 벗어난 인덱스처리
            try:
                nextX = x + tetromino[i][j][0]
                nextY = y + tetromino[i][j][1]
                result += board[nextX][nextY] # 누적
            except IndexError: 
                continue
        answer = max(answer, result) # 최대값 갱신
        

for i in range(n):
    for j in range(m):
        find(i, j)

print(answer)

try, except를 알고리즘 때 사용을 잘 안했지만 이 문제에 대단히 유용.

참고)
https://jeongchul.tistory.com/670
https://choichumji.tistory.com/98

좋은 웹페이지 즐겨찾기