강의로 돌아가기
myeongjunkim

파이썬 구현 풀이

l_index 정의

-직사각형의 밑변은 0, 우변은 1, 윗변은 2, 좌변은 3 으로 l_index 를 정의한다.

  • 반시계방향으로 회전할 때 l_index 는 항상 0->3->2->1->0 ,,, 의 변화 규칙을 가지며 회전한다.
  • 교점에서 위 규칙에 따라 l_index 를 변경한다.
작성중인 코드―solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
5:00 ~ 

조건:
    - 꼭짓점에서 만나거나, 변이 겹치는 경우 등은 없습니다.
    - 지형이 2개 이상으로 분리된 경우도 없습니다.
    - 완전히 포함되는 경우 또한 없습니다.
    - x축 좌표, 혹은 y축 좌표가 같은 경우는 없습니다.
    - rectangle 개수는 1~4
    - rectangle [좌측 하단 x, 좌측 하단 y, 우측 상단 x, 우측 상단 y]
    - 직사각형을 나타내는 모든 좌표값은 1 이상 50 이하인 자연수
    - charcterX, charcterY는 1 이상 50 이하인 자연수
    - itemX, itemY는 1 이상 50 이하인 자연수
구현:
    - 항상 반시계방향으로 회전
    - item 좌표와 같아지면 거리 체크 후 원점 도착까지 진행하여 거리 체크
    - move(point) 메소드 구현
        - 다음 좌표 리턴
        - 직사각형의 꼭지점이면 다른 변으로
        - 교점이면 다른 변으로
    - check(point)
        - 출발 사각형 및 출발 라인 리턴
예외:
    - 
"""

def solution(rectangle, characterX, characterY, itemX, itemY):
    def check(current, rectangle):
        result = []
        point_x, point_y = current
        for i in range(len(rectangle)):
            x1, y1, x2, y2 = rectangle[i]
            if y1 == point_y and x1<=point_x<x2:
                result.append(0)
            if x2 == point_x and y1<=point_y<y2:
                result.append(1)
            if y2 == point_y and x1<point_x<=x2:
                result.append(2)
            if x1 == point_x and y1<point_y<=y2:
                result.append(3)

        # 교점일 경우 len(result) == 2 
        # l_index 에 우선순위 존재 0 -> 3 -> 2 -> 1 -> 0 -> ,,,
        return max(result) if set(result) == {0, 3} else min(result)


    def move(point, l_index):
        direct = [ [1,0], [0,1], [-1,0], [0,-1] ]
        dx, dy = direct[l_index]
        x, y = point
        return (x+dx, y+dy)


    start = (characterX, characterY)
    item = (itemX, itemY)
    current = (characterX, characterY)
    start_d, item_d = 0, 0
    while True:
        l_index = check(current, rectangle)
        current = move(current, l_index)
        start_d += 1
        if current == item:
            item_d = start_d
        if current == start:
            break

    return min(start_d-item_d, item_d)


  • 양가영

    따봉드립니다

    양가영―2024.03.11 20:41
  • HJ

    좋은 풀이 감사합니다~!

    HJ―2024.07.11 06:25
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다.