Baekjoon

[BOJ / 백준] 16937번 두 스티커 파이썬(Python) 문제 풀이

728x90

 

문제

 

문제 링크 : https://www.acmicpc.net/problem/16937

 

16937번: 두 스티커

첫째 줄에 모눈종이의 크기 H, W, 둘째 줄에 스티커의 수 N이 주어진다. 다음 N개의 줄에는 스티커의 크기 Ri, Ci가 주어진다.

www.acmicpc.net

 

 

 

 

 

CODE

import sys

h, w = map(int, input().split())
n = int(input())
stk = list(list(map(int, sys.stdin.readline().split())) for _ in range(n))

result = 0
for i in range(n):
    for j in range(i + 1, n):
        r1, c1 = stk[i]
        r2, c2 = stk[j]

        if (r1 + r2 <= h and max(c1, c2) <= w) or (max(r1, r2) <= h and c1 + c2 <= w):
            result = max(result, r1*c1 + r2*c2)
        if (c1 + r2 <= h and max(r1, c2) <= w) or (max(c1, r2) <= h and r1 + c2 <= w):
            result = max(result, r1*c1 + r2*c2)
        if (c1 + c2 <= h and max(r1, r2) <= w) or (max(c1, c2) <= h and r1 + r2 <= w):
            result = max(result, r1*c1 + r2*c2)
        if (r1 + c2 <= h and max(c1, r2) <= w) or (max(r1, c2) <= h and c1 + r2 <= w):
            result = max(result, r1*c1 + r2*c2)

print(result)

 

 

 

 

 

 

풀이

스티커 중 두 개를 골라 다음 조건에 맞는지 검사한다

if (r1 + r2 <= h and max(c1, c2) <= w) or (max(r1, r2) <= h and c1 + c2 <= w):
    result = max(result, r1*c1 + r2*c2)

 

 

여기서 스티커가 회전하는 경우는 다음의 네 가지 경우를 고려할 수 있다

둘 다 회전하지 않은 경우, 첫 번째 스티커만 회전한 경우, 두 번째 스티커만 회전한 경우, 둘 다 회전한 경우

둘 다 회전하지 않은 경우
첫 번째 스티커만 회전한 경우
두 번째 스티커만 회전한 경우
둘 다 회전한 경우

 

 

 

 

결과

 

 

 

 

 

 

 

728x90