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
"""
๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ

๐Ÿ’› ๋ฌธ์ œ
ROR ๊ฒŒ์ž„์€ ๋‘ ํŒ€์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ง„ํ–‰ํ•˜๋ฉฐ, ์ƒ๋Œ€ ํŒ€ ์ง„์˜์„ ๋จผ์ € ํŒŒ๊ดดํ•˜๋ฉด ์ด๊ธฐ๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ, ๊ฐ ํŒ€์€ ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ์ตœ๋Œ€ํ•œ ๋นจ๋ฆฌ ๋„์ฐฉํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๊ฒŒ์ž„ ๋งต์˜ ์ƒํƒœ maps๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ,
์บ๋ฆญํ„ฐ๊ฐ€ ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ๋„์ฐฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ง€๋‚˜๊ฐ€์•ผ ํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
๋‹จ, ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ๋„์ฐฉํ•  ์ˆ˜ ์—†์„ ๋•Œ๋Š” -1์„ return ํ•ด์ฃผ์„ธ์š”.

๐Ÿงก ์ œํ•œ ์‚ฌํ•ญ
maps๋Š” n x m ํฌ๊ธฐ์˜ ๊ฒŒ์ž„ ๋งต์˜ ์ƒํƒœ๊ฐ€ ๋“ค์–ด์žˆ๋Š” 2์ฐจ์› ๋ฐฐ์—ด๋กœ, n๊ณผ m์€ ๊ฐ๊ฐ 1 ์ด์ƒ 100 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
    n๊ณผ m์€ ์„œ๋กœ ๊ฐ™์„ ์ˆ˜๋„, ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, n๊ณผ m์ด ๋ชจ๋‘ 1์ธ ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
maps๋Š” 0๊ณผ 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, 0์€ ๋ฒฝ์ด ์žˆ๋Š” ์ž๋ฆฌ, 1์€ ๋ฒฝ์ด ์—†๋Š” ์ž๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
์ฒ˜์Œ์— ์บ๋ฆญํ„ฐ๋Š” ๊ฒŒ์ž„ ๋งต์˜ ์ขŒ์ธก ์ƒ๋‹จ์ธ (1, 1) ์œ„์น˜์— ์žˆ์œผ๋ฉฐ, ์ƒ๋Œ€๋ฐฉ ์ง„์˜์€ ๊ฒŒ์ž„ ๋งต์˜ ์šฐ์ธก ํ•˜๋‹จ์ธ (n, m) ์œ„์น˜์— ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’š ์ž…์ถœ๋ ฅ
maps	                                                        answer
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]]	11
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,0],[0,0,0,0,1]]	-1
"""

from collections import deque

def bfs(maps, x, y):

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

    queue = deque()
    queue.append((x, y))

    while queue:
        x, y = queue.popleft()

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

            if nx < 0 or nx >= len(maps) or ny < 0 or ny >= len(maps[0]):
                continue
            # ๋ฒฝ
            if maps[nx][ny] == 0:
                continue

            # ์ฒ˜์Œ ์ง€๋‚˜๊ฐ€๋Š” ๊ธธ + ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
            if maps[nx][ny] == 1:
                maps[nx][ny] = maps[x][y] + 1
                queue.append((nx, ny))

    # ์ƒ๋Œ€ํŽธ ์ขŒํ‘œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ (n, n)
    return maps[len(maps)-1][len(maps[0])-1]

# maps๋Š” 0๊ณผ 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, 0์€ ๋ฒฝ์ด ์žˆ๋Š” ์ž๋ฆฌ, 1์€ ๋ฒฝ์ด ์—†๋Š” ์ž๋ฆฌ
def solution(maps):
    answer = 0
    answer = bfs(maps, 0, 0)

    if answer == 1 :
        return -1
    else :
        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
    
      ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
      ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
      ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 10 ใ€‰ ํ†ต๊ณผ (0.13ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 11 ใ€‰ ํ†ต๊ณผ (0.04ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 12 ใ€‰ ํ†ต๊ณผ (0.04ms, 10.1MB)
      ํ…Œ์ŠคํŠธ 13 ใ€‰ ํ†ต๊ณผ (0.05ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 14 ใ€‰ ํ†ต๊ณผ (0.04ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 15 ใ€‰ ํ†ต๊ณผ (0.04ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 16 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 17 ใ€‰ ํ†ต๊ณผ (0.12ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 18 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 19 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 20 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 21 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.4MB)
      ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
      ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (15.65ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (4.29ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (11.20ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (6.64ms, 10.3MB)
      ์ฑ„์  ๊ฒฐ๊ณผ
      ์ •ํ™•์„ฑ: 69.9
      ํšจ์œจ์„ฑ: 30.1
      ํ•ฉ๊ณ„: 100.0 / 100.0
    


3. ํ•ด์„ค

  • ์ƒ๋Œ€ํŒ€ ์ง„์˜๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๊ธฐ
  • ๋งˆ์ง€๋ง‰ ์ƒ๋Œ€ํŒ€ ์ง„์˜์˜ ์ตœ๋Œ€ ๊ฑฐ๋ฆฌ return ํ•˜๊ธฐ
This post is licensed under CC BY 4.0 by the author.