Post

๐Ÿข ์•„์ดํ…œ ์ค๊ธฐ

1. ๋ฌธ์ œ ๋งํฌ

์•„์ดํ…œ ์ค๊ธฐ


2. ์ฝ”๋“œ

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
์•„์ดํ…œ ์ค๊ธฐ

๐Ÿ’› ๋ฌธ์ œ
๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹ค๊ฐํ˜• ๋ชจ์–‘ ์ง€ํ˜•์—์„œ ์บ๋ฆญํ„ฐ๊ฐ€ ์•„์ดํ…œ์„ ์ค๊ธฐ ์œ„ํ•ด ์ด๋™ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.
์ง€ํ˜•์€ ๊ฐ ๋ณ€์ด x์ถ•, y์ถ•๊ณผ ํ‰ํ–‰ํ•œ ์ง์‚ฌ๊ฐํ˜•์ด ๊ฒน์ณ์ง„ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ, ์บ๋ฆญํ„ฐ๋Š” ์ด ๋‹ค๊ฐํ˜•์˜ ๋‘˜๋ ˆ(๊ตต์€ ์„ )๋ฅผ ๋”ฐ๋ผ์„œ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ง์‚ฌ๊ฐํ˜•์„ ๊ฒน์นœ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค‘์•™์— ๋นˆ ๊ณต๊ฐ„์ด ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ, ๋‹ค๊ฐํ˜•์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ ํ…Œ๋‘๋ฆฌ๊ฐ€ ์บ๋ฆญํ„ฐ์˜ ์ด๋™ ๊ฒฝ๋กœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
๋‹จ, ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์ง์‚ฌ๊ฐํ˜•์˜ x์ถ• ์ขŒํ‘œ ๋˜๋Š” y์ถ• ์ขŒํ‘œ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
์ฆ‰, ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์ง์‚ฌ๊ฐํ˜•์ด ๊ผญ์ง“์ ์—์„œ ๋งŒ๋‚˜๊ฑฐ๋‚˜, ๋ณ€์ด ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ ๋“ฑ์€ ์—†์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง€ํ˜•์ด 2๊ฐœ ์ด์ƒ์œผ๋กœ ๋ถ„๋ฆฌ๋œ ๊ฒฝ์šฐ๋„ ์—†์Šต๋‹ˆ๋‹ค.
ํ•œ ์ง์‚ฌ๊ฐํ˜•์ด ๋‹ค๋ฅธ ์ง์‚ฌ๊ฐํ˜• ์•ˆ์— ์™„์ „ํžˆ ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ ๋˜ํ•œ ์—†์Šต๋‹ˆ๋‹ค.

์ง€ํ˜•์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ง์‚ฌ๊ฐํ˜•์ด ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด rectangle, ์ดˆ๊ธฐ ์บ๋ฆญํ„ฐ์˜ ์œ„์น˜ characterX, characterY,
์•„์ดํ…œ์˜ ์œ„์น˜ itemX, itemY๊ฐ€ solution ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ,
์บ๋ฆญํ„ฐ๊ฐ€ ์•„์ดํ…œ์„ ์ค๊ธฐ ์œ„ํ•ด ์ด๋™ํ•ด์•ผ ํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

๐Ÿงก ์ œํ•œ ์‚ฌํ•ญ
rectangle์˜ ์„ธ๋กœ(ํ–‰) ๊ธธ์ด๋Š” 1 ์ด์ƒ 4 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
rectangle์˜ ์›์†Œ๋Š” ๊ฐ ์ง์‚ฌ๊ฐํ˜•์˜ [์ขŒ์ธก ํ•˜๋‹จ x, ์ขŒ์ธก ํ•˜๋‹จ y, ์šฐ์ธก ์ƒ๋‹จ x, ์šฐ์ธก ์ƒ๋‹จ y] ์ขŒํ‘œ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค.
    ์ง์‚ฌ๊ฐํ˜•์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ชจ๋“  ์ขŒํ‘œ๊ฐ’์€ 1 ์ด์ƒ 50 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
    ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์ง์‚ฌ๊ฐํ˜•์˜ x์ถ• ์ขŒํ‘œ, ํ˜น์€ y์ถ• ์ขŒํ‘œ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
    ๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋งž๋Š” ์ง์‚ฌ๊ฐํ˜•๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
charcterX, charcterY๋Š” 1 ์ด์ƒ 50 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
    ์ง€ํ˜•์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹ค๊ฐํ˜• ํ…Œ๋‘๋ฆฌ ์œ„์˜ ํ•œ ์ ์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
itemX, itemY๋Š” 1 ์ด์ƒ 50 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
    ์ง€ํ˜•์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹ค๊ฐํ˜• ํ…Œ๋‘๋ฆฌ ์œ„์˜ ํ•œ ์ ์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
์บ๋ฆญํ„ฐ์™€ ์•„์ดํ…œ์˜ ์ฒ˜์Œ ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
์ „์ฒด ๋ฐฐ์ ์˜ 50%๋Š” ์ง์‚ฌ๊ฐํ˜•์ด 1๊ฐœ์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
์ „์ฒด ๋ฐฐ์ ์˜ 25%๋Š” ์ง์‚ฌ๊ฐํ˜•์ด 2๊ฐœ์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
์ „์ฒด ๋ฐฐ์ ์˜ 25%๋Š” ์ง์‚ฌ๊ฐํ˜•์ด 3๊ฐœ ๋˜๋Š” 4๊ฐœ์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

๐Ÿ’š ์ž…์ถœ๋ ฅ
rectangle	                                characterX	characterY	itemX	itemY	result
[[1,1,7,4],[3,2,5,5],[4,3,6,9],[2,6,8,8]]	1	        3	        7	    8	    17
[[1,1,8,4],[2,2,4,9],[3,6,9,8],[6,3,7,7]]	9	        7	        6	    1	    11
[[1,1,5,7]]	                                1	        1	        4	    7	    9
[[2,1,7,5],[6,4,10,10]]	                    3	        1	        7	    10	    15
[[2,2,5,5],[1,3,6,4],[3,1,4,6]]	            1	        4	        6	    3	    10
"""

from collections import deque

# ์ง€ํ˜•์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ง์‚ฌ๊ฐํ˜•์ด ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด rectangle, ์ดˆ๊ธฐ ์บ๋ฆญํ„ฐ์˜ ์œ„์น˜ characterX, characterY, ์•„์ดํ…œ์˜ ์œ„์น˜ itemX, itemY
def solution(rectangle, characterX, characterY, itemX, itemY):
    answer = 0

    # ๋ชจ๋“  ์ขŒํ‘œ๊ฐ’์€ 1 ์ด์ƒ 50 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜
    graph = [[-1] * 102 for i in range(102)]

    for r in rectangle:
        x1, y1, x2, y2 = r[0] * 2, r[1] * 2, r[2] * 2, r[3] * 2

        for i in range(x1, x2+1):
            for j in range(y1, y2+1):

                # ๋‚ด๋ถ€์ผ ๋•Œ
                if x1 < i < x2 and y1 < j < y2:
                    graph[i][j] = 0
                # ํ…Œ๋‘๋ฆฌ์ผ ๋•Œ
                elif graph[i][j] != 0:
                    graph[i][j] = 1

    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]

    q = deque()
    # ์ฒซ ์‹œ์ž‘
    q.append([characterX * 2, characterY * 2])
    visited = [[0] * 102 for _ in range(102)]
    visited[characterX * 2][characterY * 2] = 1

    # ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์ฐพ๊ธฐ
    while q:
        x, y = q.popleft()

        # ์บ๋ฆญํ„ฐ์™€ ์•„์ดํ…œ ์œ„์น˜๊ฐ€ ๊ฐ™์„ ๋•Œ
        if x == itemX * 2 and y == itemY * 2:
            answer = (visited[x][y] - 1) // 2
            break

        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]

            # ๋ฐฉ๋ฌธ๋„ ์•ˆํ–ˆ๊ณ , ํ…Œ๋‘๋ฆฌ์ธ ๊ฒฝ์šฐ
            if graph[nx][ny] == 1 and visited[nx][ny] == 0:
                q.append([nx, ny])
                # ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์ฆ๊ฐ€
                visited[nx][ny] = visited[x][y] + 1

    return answer


  • ์ •ํ™•์„ฑ

    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
    
      ์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
      ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.23ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.23ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (2.56ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (1.58ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (1.11ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (1.41ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (1.22ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.73ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.34ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (1.21ms, 10.1MB)
      ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.91ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.57ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (1.19ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (1.43ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.63ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.39ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.89ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.70ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.26ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.43ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.31ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.31ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.32ms, 10.5MB)
      ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.47ms, 10.3MB)
      ์ฑ„์  ๊ฒฐ๊ณผ
      ์ •ํ™•์„ฑ: 100.0
      ํ•ฉ๊ณ„: 100.0 / 100.0
    


3. ํ•ด์„ค

  • ํ…Œ๋‘๋ฆฌ๊ฐ€ 1 1 ์ด๋ ‡๊ฒŒ ๊ฒน์น  ๊ฒฝ์šฐ ์ค‘๊ฐ„์— ํ‹ˆ์ด ์—†์–ด์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ์— ์ง€์žฅ์„ ์คŒ
  • 2๋ฐฐ๋กœ ๊ธธ์ด๋ฅผ ๋„“ํ˜€์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ํ‹ˆ์„ ์ค€๋‹ค
  • ๊ทธ ์™ธ์—๋Š” ๋˜‘๊ฐ™์ด ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์•„์ดํ…œ๊ณผ ์œ„์น˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๊ฒฝ๋กœ์—์„œ 2๋ฅผ ๋‚˜๋ˆˆ ๊ฐ’์ด ๋‹ต์ด ๋œ๋‹ค
This post is licensed under CC BY 4.0 by the author.