백준 :: 참외밭 <2477번>

혜 콩·2022년 5월 30일
1

알고리즘

목록 보기
22/61

> 문제 <

https://www.acmicpc.net/problem/2477

> 아이디어 <

# 반시계방향   # 큰사각형 - 작은사각형

❓ 빼야될 작은 직사각형의 가로 변, 세로 변을 어떻게 알 수 있을까?
→ 여러 가지 경우의 수의 그림을 그려보고 관계성을 파악해보니,
계단식 모양, 즉 (1, 3, 1, 3), (2, 4, 2, 4) 등 방향이 2회씩 반복된다. (빨간색 선)
작은 사각형의 가로, 세로 변은 반복 속에 존재하는 중간 부분이다.
왜냐하면, 이전 방향과 다음 방향의 (i-1, i+1) 방향이 같다면 작은 사각형의 가로 혹은 세로 변이기 때문. (1, 3, 1, 3)

> 코드 <

# mine (success)
# 1: 동쪽, 2: 서쪽, 3: 남쪽, 4: 북쪽

import sys

K = int(sys.stdin.readline())
field=[]
for i in range(6):
    field.append(list(map(int, input().split())))        # [4,50] = [변의 방향, 길이]

lm = 1
wm = 1
for i in range(6):
    if field[i][0] == 4 or field[i][0] == 3:
        if lm < field[i][1]: lm = field[i][1]
        largeLen = lm                                # 큰 사각형 (가장 긴 세로 변)
    else:
        if wm < field[i][1]: wm = field[i][1]
        largeWid = wm                                # 가장 긴 가로 변

bigSquare = largeWid * largeLen
small = []

for i in range(6):
    if i == 0:
        if field[-1][0] == field[i+1][0]:
            small.append(field[i][1])                       # 작은 사각형의 가로(세로)변
    elif i == 5:
        if field[i-1][0] == field[0][0]:
            small.append(field[i][1])                       # 작은 사각형의 가로(세로)변
    else:
        if field[i-1][0] == field[i+1][0]:
            small.append(field[i][1])                       # 작은 사각형의 가로(세로)변

smallSquare = 1
for i in small:
    smallSquare *= i

fieldArea = bigSquare - smallSquare
print(fieldArea * K)
profile
배우고 싶은게 많은 개발자📚

0개의 댓글