백준 14500 테트로미노

1621 단어 재귀재귀
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
paper = [list(map(int, input().split())) for _ in range(n)]
# 기본 테트로미노
tmp = [
    [(0, 0), (0, 1), (0, 2), (0, 3)],
    [(0, 0), (0, 1), (1, 0), (1, 1)],
    [(0, 0), (1, 0), (2, 0), (2, 1)],
    [(0, 0), (1, 0), (1, 1), (2, 1)],
    [(0, 0), (0, 1), (0, 2), (1, 1)]
]
tetromino = []

def rotate(l, cnt, type):
    global tetromino
    if cnt == 4:
        return
    nl = l[:]
    # 회전
    for i in range(1, 4):
        u, v = l[i]
        nl[i] = (v, -u)
    tetromino.append(nl)
    rotate(nl, cnt + 1, type)
    if type == 0:
        return
    # 상하 대칭
    nnl = nl[:]
    for i in range(1, 4):
        u, v = nl[i]
        nnl[i] = (-u, v)
    tetromino.append(nnl)
    # 좌우 대칭
    nnnl = nnl[:]
    for i in range(1, 4):
        u, v = nl[i]
        nnnl[i] = (u, -v)
    tetromino.append(nnnl)

for i in range(5):
    if i != 1:
        rotate(tmp[i], 0, i)
    else:
        tetromino.append(tmp[i])

ans = 0
for i in range(n):
    for j in range(m):
        x, y = i, j
        for u in range(len(tetromino)):
            t_ans = 0
            flag = 0
            for v in range(4):
                nx, ny = x + tetromino[u][v][0], y + tetromino[u][v][1]
                # 벗어날 때 
                if nx < 0 or ny < 0 or nx >= n or ny >= m:
                    flag = 1
                    break
                t_ans += paper[nx][ny]
            if flag == 0:
                ans = max(ans, t_ans)
                
print(ans)

테트로미노를 모두 구하고 칸마다 넣어봄

좋은 웹페이지 즐겨찾기