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
from collections import deque
ground = [[0] * 102 for i in range(102)]
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

def solution(rectangle, characterX, characterY, itemX, itemY):
    # ์บ๋ฆญํ„ฐ, ์•„์ดํ…œ ์ขŒํ‘œ 2๋ฐฐ๋กœ ๋Š˜๋ฆฌ๊ธฐ
    cx, cy, ix, iy = characterX*2, characterY*2, itemX*2, itemY*2

    # ์ขŒํ‘œ ์ง์‚ฌ๊ฐํ˜• ํ‘œ์‹œ
    for r in rectangle:
        # ์ขŒํ‘œ๋ฅผ 2๋ฐฐ๋กœ ๋Š˜๋ฆฌ๊ธฐ
        x1, y1, x2, y2 = map(lambda x: x*2, r)
        for i in range(x1, x2+1):
            for j in range(y1, y2+1):
                # ์ง์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€ 2๋กœ ์ฑ„์šฐ๊ธฐ
                if x1 < i < x2 and y1 < j < y2:
                    ground[i][j] = 2
                # ํ…Œ๋‘๋ฆฌ 1๋กœ ์ฑ„์šฐ๊ธฐ
                elif ground[i][j] != 2:
                    ground[i][j] = 1 

    q = deque([(cx, cy)])
    distance = [[1] * 102 for i in range(102)]
    while q:
        x, y = q.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            # ๋ฒ”์œ„ ๋ฐ–์œผ๋กœ ๋‚˜๊ฐˆ ์‹œ ์žฌํƒ์ƒ‰
            if nx < 0 or nx > 101 or ny < 0 or ny > 101:
                continue
            # ํ…Œ๋‘๋ฆฌ์— ํฌํ•จ๋˜๋ฉด์„œ ๋ฐฉ๋ฌธ ์•ˆ ํ–ˆ์„ ๊ฒฝ์šฐ
            if ground[nx][ny] == 1 and distance[nx][ny] == 1:
                distance[nx][ny] = distance[x][y] + 1
                q.append((nx, ny)) 

    return distance[ix][iy] // 2

  • ์ •ํ™•์„ฑ

    ํ…Œ์ŠคํŠธ 1 ใ€‰ํ†ต๊ณผ (0.10ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 2 ใ€‰ํ†ต๊ณผ (0.15ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 3 ใ€‰ํ†ต๊ณผ (0.15ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 4 ใ€‰ํ†ต๊ณผ (0.10ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 5 ใ€‰ํ†ต๊ณผ (0.11ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 6 ใ€‰ํ†ต๊ณผ (0.10ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 7 ใ€‰ํ†ต๊ณผ (0.18ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 8 ใ€‰ํ†ต๊ณผ (0.19ms, 10.6MB)
    ํ…Œ์ŠคํŠธ 9 ใ€‰ํ†ต๊ณผ (1.12ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 10 ใ€‰ํ†ต๊ณผ (1.23ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 11 ใ€‰ํ†ต๊ณผ (1.10ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 12 ใ€‰ํ†ต๊ณผ (1.18ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 13 ใ€‰ํ†ต๊ณผ (2.02ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 14 ใ€‰ํ†ต๊ณผ (0.67ms, 10.6MB)
    ํ…Œ์ŠคํŠธ 15 ใ€‰ํ†ต๊ณผ (0.45ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 16 ใ€‰ํ†ต๊ณผ (0.77ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 17 ใ€‰ํ†ต๊ณผ (0.72ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 18 ใ€‰ํ†ต๊ณผ (0.78ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 19 ใ€‰ํ†ต๊ณผ (1.22ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 20 ใ€‰ํ†ต๊ณผ (1.39ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 21 ใ€‰ํ†ต๊ณผ (1.43ms, 10.6MB)
    ํ…Œ์ŠคํŠธ 22 ใ€‰ํ†ต๊ณผ (0.56ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 23 ใ€‰ํ†ต๊ณผ (1.09ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 24 ใ€‰ํ†ต๊ณผ (0.73ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 25 ใ€‰ํ†ต๊ณผ (0.24ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 26 ใ€‰ํ†ต๊ณผ (0.27ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 27 ใ€‰ํ†ต๊ณผ (0.29ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 28 ใ€‰ํ†ต๊ณผ (0.30ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 29 ใ€‰ํ†ต๊ณผ (0.47ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 30 ใ€‰ํ†ต๊ณผ (0.57ms, 10.5MB)


3. ํ•ด์„ค

bfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์ขŒํ‘œ์ƒ์— ํ…Œ๋‘๋ฆฌ๋ฅผ 1, ์ง์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€๋ฅผ 2๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค. ๊ฒน์น˜๋Š” ๋‚ด๋ถ€๋„ 2๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.

์ด๋•Œ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ๋Š” ํ…Œ๋‘๋ฆฌ๋ฅผ ์นธ ๋‚ด๋ถ€์— ํ‘œ์‹œํ•˜๋‹ค ๋ณด๋‹ˆ ์‹ค์ œ ์ด์–ด์ ธ ์žˆ์ง€ ์•Š์€ ๊ธธ์ด ์ด์–ด์ง„ ๊ฒƒ ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค.

์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์— ํŒŒ๋ž€ ์นธ์€ ์‹ค์ œ ์ด์–ด์ง€์ง€ ์•Š์€ ์ค„์ด์ง€๋งŒ bfs๋กœ ํƒ์ƒ‰ํ•˜๋‹ค๋ณด๋ฉด ์—ฐ๊ฒฐ๋œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ํƒ์ƒ‰ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์‹ค์ œ ๋‹ต๋ณด๋‹ค ๋” ์ž‘์€ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‹ค์ œ ํฌ๊ธฐ์˜ 2๋ฐฐ๋กœ ๊ณ„์‚ฐ์„ ํ•ด์ค€๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ๊ธธ์ด ๋ถ™์–ด์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ ํ›„ bfs๋ฅผ ๋Œ๋ฉฐ ํ…Œ๋‘๋ฆฌ๋ฅผ ๋”ฐ๋ผ ํƒ์ƒ‰ํ•ด์ค€๋‹ค. ์•„์ดํ…œ ์œ„์น˜์˜ distance ๊ฐ’์„ ๋ฆฌํ„ดํ•  ๋•Œ 2๋ฐฐ๋กœ ํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 2๋กœ ๋‚˜๋ˆ„์–ด ๋ฆฌํ„ดํ•œ๋‹ค.

This post is licensed under CC BY 4.0 by the author.