PS/BOJ
[백준] 테트로미노 | 삼성
RUBY_루비
2020. 8. 29. 23:59
출처:: https://www.acmicpc.net/problem/14500
분류:: 구현, 브루트 포스
1. 문제 이해 및 해결과정
- 테트로미노의 회전까지 더해서 가능한 모양은 19가지이다 - 전체 배열을 돌면서 테트로미노의 블럭이 맞는 곳 중에 최대 값을 찾는다 |
2. 풀이방법
#테트로미노
#https://www.acmicpc.net/problem/14500
import sys
sys.stdin = open("input.txt","r")
n,m=map(int,input().split())
board=[list(map(int,input().split())) for _ in range(n)]
tetromino=[ #19가지 테트로미노가 나올 수 있음
[(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,1)], #ㅜ
[(0,1),(1,0),(1,1),(1,2)], #ㅗ
[(0,1),(1,0),(1,1),(2,1)], #ㅓ
[(0,0),(1,0),(1,1),(2,0)], #ㅏ
[(0,0),(0,1),(0,2),(1,0)], #긴 기억 모양
[(0,2),(1,0),(1,1),(1,2)],
[(0,0),(1,0),(1,1),(1,2)],
[(0,0),(0,1),(0,2),(1,2)],
[(0,0),(0,1),(1,1),(2,1)],
[(0,0),(0,1),(1,0),(2,0)],
[(0,0),(1,0),(2,0),(2,1)],
[(0,1),(1,1),(2,0),(2,1)],
[(0,1),(1,0),(1,1),(2,0)], #번개모양
[(0,0),(1,0),(1,1),(2,1)],
[(0,1),(0,2),(1,0),(1,1)],
[(0,0),(0,1),(1,1),(1,2)]
]
res=0
def compute(h,w):
global res
for i in range(19): #테트로미노 19개
sum=0
for j in range(4): #테트로미노 블록 수
try:
nh=h+tetromino[i][j][0] #테트로미노 중 한 블록의 x좌표
nw=w+tetromino[i][j][1] #테트로미노 중 한 블록의 y좌표
sum+=board[nh][nw]
except IndexError: #테트로미노가 board밖으로 나갈 경우
continue
res=max(res,sum)
for i in range(n):
for j in range(m):
compute(i,j)
print(res)
3. 오답원인
4. 알게된 점
try, catch IndexError
try:
nh=h+tetromino[i][j][0] #테트로미노 중 한 블록의 x좌표
nw=w+tetromino[i][j][1] #테트로미노 중 한 블록의 y좌표
sum+=board[nh][nw]
except IndexError: #테트로미노가 board밖으로 나갈 경우
continue