아이공의 AI 공부 도전기

[Baekjoon] 2477번 : 참외밭 (Python, 기하학)

 

     

 

 

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

 

2477번: 참외밭

첫 번째 줄에 1m2의 넓이에 자라는 참외의 개수를 나타내는 양의 정수 K (1 ≤ K ≤ 20)가 주어진다. 참외밭을 나타내는 육각형의 임의의 한 꼭짓점에서 출발하여 반시계방향으로 둘레를 돌면서 지

www.acmicpc.net

 

코드 링크

https://github.com/stellaluminary/Baekjoon

 

GitHub - stellaluminary/Baekjoon

Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.

github.com

 

Python

 

방법 1 - 메모리 30840KB / 시간 72ms / 코드 길이 694B

 

ㄱ을 기준으로 90도씩 회전의 경우 총 4가지의 모양이 있습니다. (ㄱ,┏, ┗, ┛)

여기서 반시계방향으로 측정하고 이에 따라 각 모양마다의 특징이 존재합니다.

ㄱ은 동쪽 2회, 남쪽 2회

┏은 동쪽 2회, 북쪽 2회

┛은 서쪽 2회, 남쪽 2회

┗은 서쪽 2회, 북쪽 2회

를 이동해야 합니다.

따라서 최대 길이를 기준으로 각 모양마다의 특징을 살린 합 차이를 구해 값을 계산합니다.

 

 

n = int(input())
w, h = [],[]
res = 0
direction = {i:0 for i in range(1,5)}

for i in range(6):
    d, l = map(int, input().split())
    direction[d] += 1
    if d == 1 or d == 2:
        w.append(l)
    elif d == 3 or d == 4:
        h.append(l)

widx = w.index(max(w))
hidx = h.index(max(h))
if direction[1] == 2 and direction[3] == 2:
    s = max(w) * max(h) - w[widx - 2] * h[hidx - 1]
elif direction[1] == 2 and direction[4] == 2:
    s = max(w) * max(h) - w[widx - 1] * h[hidx - 2]
elif direction[2] == 2 and direction[3] == 2:
    s = max(w) * max(h) - w[widx - 1] * h[hidx - 2]
elif direction[2] == 2 and direction[4] == 2:
    s = max(w) * max(h) - w[widx - 2] * h[hidx - 1]
print(s*n)

 

방법 2 - 메모리 30840KB / 시간 72ms / 코드 길이 521B

 

방법 1에서는 각 모양에 따른 별도의 조건문을 사용했다면 방법 2에서는 인접한 변을 제외한 변의 곱을 빼는 방법을 채택한다.

 

예를들어 가장 긴 너비 w가 있다고 가정하면 순서상 w의 이전과 이후는 높이에 해당하는 것이고 이들은 작은 사각형을 빼는데 도움을 주지 않는다. 가장 긴 높이 h 역시 마찬가지로 인접한 순서 상의 두 변은 너비와 관련된 것이다.

 

본 방법은 가장 긴 너비 w와 가장 긴 높이 h에 인접하지 않은 변 간의 곱을 큰 사각형에서 빼는 것으로 그 값을 구한다.

 

 

n = int(input())
mw, mh = 0, 0
mw_idx, mh_idx = 0, 0
l=[]
for i in range(6):
    tmp = list(map(int, input().split()))
    l.append(tmp)
    if tmp[0] == 1 or tmp[0] == 2:
        if mw < tmp[1]:
            mw = tmp[1]
            mw_idx = i
    elif tmp[0] == 3 or tmp[0] == 4:
        if mh < tmp[1]:
            mh = tmp[1]
            mh_idx = i

adj = [l[mw_idx-1], l[(mw_idx+1)%6], l[mh_idx-1], l[(mh_idx+1)%6]]
m_area = 1
for i in l:
    if i not in adj:
        m_area *= i[1]
res = (mw*mh - m_area)*n
print(res)

 

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading