For Programmer

백준 2564번 파이썬 문제풀이(경비원) 본문

코팅테스트/백준 문제 모음

백준 2564번 파이썬 문제풀이(경비원)

유지광이 2022. 2. 3. 00:47
728x90


아무리 봐도 좋은 코드가 생각이 안나서 그냥 무지성 하드구현 했다... 답을 보니 북쪽을 기준으로 0 위치에서 쭉 사각형을 펼처서 절대거리를 찾는 답이 있어서 해당 코드도 한번 구현해보았다.

 

우선 무지성 하드 구현코드 이다.

 

width, height = map(int, input().split())
N = int(input())
location = []  # c,r 좌표

for i in range(N + 1):
    direct, loc = map(int, input().split())
    if direct == 1:  # 북
        location.append([loc, height])
    elif direct == 2:  # 남
        location.append([loc, 0])
    elif direct == 3:  # 서
        location.append([0, height - loc])
    elif direct == 4:  # 동
        location.append([width, height - loc])

c_l = location.pop()  # 동근이의 현재위치를 리스트에서 빼서 저장
result = 0
for i in location:
    if c_l[1] == height or c_l[1] == 0:  # 서있는 위치가 위 혹은 아래일때
        if i[1] == height or i[1] == 0:  # 상점이 남 혹은 북에 있을때
            if i[1] == c_l[1]:  # 만약 같은 라인이라면
                result += abs(i[0] - c_l[0])
            elif i[1] != c_l[1]:  # 반대편 라인이라면
                result += min(c_l[0] + height + i[0], width - c_l[0] + height + width - i[0])
        elif i[0] == 0 or i[0] == width:  # 상점이 서 동에 있을때
            if c_l[1] == height:  # 동근이 위치가 꼭대기 일때
                if i[0] == 0:  # 만약 상점이 서쪽이라면
                    result += height - i[1] + c_l[0]
                elif i[0] == width:  # 상점이 동쪽이라면
                    result += width - c_l[0] + height - i[1]
            elif c_l[1] == 0:  # 동근이 위치가 맨아래일때
                if i[0] == 0:  # 만약 상점이 서쪽이라면
                    result += c_l[0] + i[1]
                elif i[0] == width:  # 상점이 동쪽이라면
                    result += i[1] + width - c_l[0]

    elif c_l[0] == 0 or c_l[0] == width:  # 서있는 위치가 동 혹은 서 일때
        if i[0] == 0 or i[0] == width:  # 상점이 동 혹은 서에 있을때
            if i[0] == c_l[0]:  # 만약 같은 라인이라면
                result += abs(i[1] - c_l[1])
            elif i[0] != c_l[0]:  # 만약 반대편 라인이라면
                result += min(c_l[1] + width + i[1], height - c_l[1] + width + height - i[1])
        elif i[1] == 0 or i[1] == height:  # 상점이 남 북에 있다면
            if c_l[0] == 0:  # 동근이 위치가 서쪽이라면
                if i[1] == height:  # 상점이 북쪽에 있다면
                    result += height - c_l[1] + i[0]
                elif i[1] == 0:  # 상점이 남쪽에 있다면
                    result += c_l[1] + i[0]
            elif c_l[0] == width:  # 동근이 위치가 동쪽이라면
                if i[1] == height:  # 상점이 북쪽에 있다면
                    result += height - c_l[1] + width - i[0]
                elif i[1] == 0:  # 상점이 남쪽에 있다면
                    result += c_l[1] + width - i[0]

print(result)

 

다음은 사각형을 펼친 후 2개의 거리를 구해 작은 값을 계속 더해가며 문제를 깔끔하게 해결하는 코드이다.

#북쪽 왼쪽 모서리를 0으로 잡고 일자로 펼쳤을 때 0으로부터 얼마나 떨어졌는 지 위치를 계산해주는 함수
def get_distance(x, y):
    if x == 1:  # 북
        return y
    if x == 2:  # 남
        return w + h + w - y
    if x == 3:  # 서
        return w + h + w + h - y
    if x == 4:  # 동
        return w + y


# 입력
w, h = map(int, input().split()) #총 가로세로 길이 입력
n = int(input()) #상점 개수 입력

# 풀이
course = []
for _ in range(n + 1):  # 북쪽 (0) 에서 상점까지의 거리
    x, y = map(int, input().split())
    course.append(get_distance(x, y))

answer = 0

for i in range(n):  # 동근이와 상점 사이의 최단거리
    in_course = abs(course[-1] - course[i]) #정방향으로 거리를 구할때의 거리차를 구한다.
    out_course = 2 * (w + h) - in_course #반대로 거리를 구할때의 거리차를 구한다.
    answer += min(in_course, out_course) # 그 두개중 작은 값을 더해준다.

# 출력
print(answer)
728x90
Comments