백준 14500 : 테트로미노 (Python)

김현준·2023년 2월 28일

백준

목록 보기
190/214

본문 링크

import sys
input=sys.stdin.readline

L_I=lambda:list(map(int,input().split()))
I_S=lambda:map(int,input().split())
V_L_I=lambda:[ L_I() for _ in range(N) ]
N,M=I_S()
graph=V_L_I()

def A():
    tmp = 0

    for i in range(N):
        for j in range(M):
            if j>=3:
                tmp=max(graph[i][j-3]+graph[i][j-2]+graph[i][j-1]+graph[i][j] , tmp)
            if i>=3:
                tmp=max(graph[i-3][j]+graph[i-2][j]+graph[i-1][j]+graph[i][j] , tmp)

    return tmp

def B():
    tmp=0
    for i in range(N):
        for j in range(M):
            if i>=1 and j>=1:
                tmp=max(graph[i][j]+graph[i-1][j]+graph[i][j-1]+graph[i-1][j-1] , tmp)
    return tmp

def C():

    tmp=0

    for i in range(N):
        for j in range(M):
            if i>=2 and j>=1:
                tmp=max(graph[i][j-1]+graph[i-1][j-1]+graph[i-2][j-1]+graph[i][j]
                        ,graph[i-2][j-1]+graph[i-2][j]+graph[i-1][j]+graph[i][j]
                        ,graph[i-2][j]+graph[i-1][j]+graph[i][j]+graph[i][j-1]
                        ,graph[i-2][j-1]+graph[i-1][j-1]+graph[i][j-1]+graph[i-2][j]
                        ,tmp)
            if i>=1 and j>=2:
                tmp=max(graph[i-1][j]+graph[i][j-2]+graph[i][j-1]+graph[i][j] ,
                        graph[i-1][j-2]+graph[i-1][j-1]+graph[i-1][j]+graph[i][j-2]
                        ,graph[i-1][j-2]+graph[i][j-2]+graph[i][j-1]+graph[i][j]
                        ,graph[i-1][j-2]+graph[i-1][j-1]+graph[i-1][j]+graph[i][j]
                        ,tmp)
    return tmp


def D():
    tmp=0
    for i in range(N):
        for j in range(M):
            if i>=2 and j>=1:
                tmp=max(graph[i-2][j-1]+graph[i-1][j-1]+graph[i-1][j]+graph[i][j]
                        , graph[i-2][j]+graph[i-1][j]+graph[i-1][j-1]+graph[i][j-1]
                        , tmp)
            if i>=1 and j>=2:
                tmp=max(graph[i-1][j-1]+graph[i-1][j]+graph[i][j-2]+graph[i][j-1]
                        ,graph[i-1][j-2]+graph[i-1][j-1]+graph[i][j-1]+graph[i][j]
                        ,tmp)
    return tmp

def E():
    tmp=0
    for i in range(N):
        for j in range(M):
            if i>=1 and j>=2:
                tmp=max(graph[i-1][j-1]+graph[i][j-2]+graph[i][j-1]+graph[i][j]
                        ,graph[i-1][j-2]+graph[i-1][j-1]+graph[i-1][j]+graph[i][j-1]
                        ,tmp)
            if i>=2 and j>=1:
                tmp=max(graph[i-2][j-1]+graph[i-1][j-1]+graph[i][j-1]+graph[i-1][j]
                        ,graph[i-2][j]+graph[i-1][j]+graph[i][j]+graph[i-1][j-1]
                        ,tmp)
    return tmp

print(max(A() , B() , C() , D() , E() ))

📌 어떻게 접근할 것인가?

테트로미노의 5개의 도형을 그래프에 두었을때 그때의 값의 합에 최대값을 구하면 됩니다.
딱히 어려운건없고 그저 인덱스관리만 잘해주면서 구현해주면됩니다.

저는 이렇게 직접 그려가면서 구현했습니다.
상하좌우 , 대칭했을때의 경우의 수를 모두 구현해주면됩니다.

profile
울산대학교 IT융합학부

0개의 댓글