[백준] 16937번 두 스티커 ★

거북이·2023년 9월 26일
0

백준[실버3]

목록 보기
91/92
post-thumbnail

💡문제접근

  • for문을 통한 이중 반복문으로 두 개의 스티커를 선택한 다음 총 4가지의 경우를 고려해서 문제를 해결했다. 머릿속으로는 이해했지만 코드로 구현하는 과정에서 많은 시간이 걸렸던 문제였다.

💡코드(메모리 : 31256KB, 시간 : 52ms)

import sys
input = sys.stdin.readline

H, W = map(int, input().strip().split())
N = int(input())
sticker = [list(map(int, input().strip().split())) for _ in range(N)]

Max = -1
for i in range(N):
    r1, c1 = sticker[i]
    for j in range(i+1, N):
        r2, c2 = sticker[j]

        # 두 개의 스티커를 회전하지 않은 경우
        if (r1 + r2 <= H and max(c1, c2) <= W) or (max(r1, r2) <= H and c1 + c2 <= W):
            Max = max(Max, (r1 * c1) + (r2 * c2))
        # 두 개의 스티커 중에서 첫 번재 스티커만 회전한 경우
        if (r2 + c1 <= H and max(r1, c2) <= W) or (max(c1, r2) <= H and r1 + c2 <= W):
            Max = max(Max, (r1 * c1) + (r2 * c2))
        # 두 개의 스티커 중에서 두 번째 스티커만 회전한 경우
        if (r1 + c2 <= H and max(c1, r2) <= W) or (max(r1, c2) <= H and c1 + r2 <= W):
            Max = max(Max, (r1 * c1) + (r2 * c2))
        # 두 개의 스티커가 모두 회전한 경우
        if (c1 + c2 <= H and max(r1, r2) <= W) or (max(c1,c2) <= H and r1 + r2 <= W):
            Max = max(Max, (r1 * c1) + (r2 * c2))

if Max == -1:
    print(0)
else:
    print(Max)

💡소요시간 : 47m

0개의 댓글