Cute Running Puppy

algorithm/Programmers

[Programmers] ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡ (Python)

R.silver 2024. 7. 11. 20:32
๋ฐ˜์‘ํ˜•

๐Ÿ“–๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/169199

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

โœจํ•ต์‹ฌ ๋‚ด์šฉ 

์ƒ, ํ•˜, ์ขŒ, ์šฐ 4๋ฐฉํ–ฅ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์„œ
๊ฒŒ์ž„ํŒ ์œ„์˜ ์žฅ์• ๋ฌผ์ด๋‚˜ ๋งจ ๋์— ๋ถ€๋”ชํž ๋•Œ๊นŒ์ง€ ๋ฏธ๋„๋Ÿฌ์ ธ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ํ•œ ๋ฒˆ์˜ ์ด๋™์œผ๋กœ ์นœ๋‹ค. 

๐Ÿค”ํ•ด๊ฒฐ ์•„์ด๋””์–ด 

์œ ํ˜•: BFS

์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ

 

R์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๋ฏธ๋„๋Ÿฌ์ ธ ์ด๋™ํ•˜๋ฉฐ G์— ๋„์ฐฉํ•ด์•ผ ํ•œ๋‹ค. 

-> ์ผ๋ฐ˜์ ์ธ ์ตœ๋‹จ๊ฑฐ๋ฆฌ BFS ๋ฌธ์ œ์—์„œ ์ด๋™ ๊ฑฐ๋ฆฌ(๋ฐฉ์‹)๋ฅผ ์กฐ์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค! 

๋งค ํ„ด ๋กœ๋ด‡์˜ ๋„์ฐฉ ์œ„์น˜ (3ํ„ด ๊นŒ์ง€ ํ‘œ์‹œ)

์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋กœ๋ด‡์˜ ๋„์ฐฉ ์œ„์น˜๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. 

๋กœ๋ด‡์˜ ํ˜„์žฌ ์œ„์น˜์—์„œ ์ƒํ•˜์ขŒ์šฐ ์ค‘ ์ด๋™ ๊ฐ€๋Šฅํ•œ ์œ„์น˜๋กœ ์ด๋™ ์‹œํ‚ค๋ฉฐ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค. 

๋„์ฐฉ ์œ„์น˜๋ฅผ visited ๋ฐฐ์—ด์— ์ด๋™ ํšŸ์ˆ˜๋กœ ํ‘œ์‹œํ•˜๋ฉฐ ์ด๋™ํ•œ๋‹ค. 

 

๐ŸŸก ์ฃผ์˜ ํ•  ์ 

    # ์ด๋™ ํ•จ์ˆ˜ - ํ•œ ๋ฒˆ์— ๋ฏธ๋Œ์–ด์ง€๋“ฏ ์ด๋™
    def move(x, y, dx, dy):
        nx, ny = x + dx, y + dy
        while in_range(nx, ny) and board[nx][ny] != 'D':
            nx += dx
            ny += dy
            
        return nx - dx, ny - dy # D ์œ„์น˜์—์„œ while ๋ฌธ์ด ์ •์ง€๋˜๋ฏ€๋กœ ํ•œ ์นธ์”ฉ ๋’ค๋กœ ๊ฐ€์•ผ ํ•œ๋‹ค.

๊ธฐ์กด ์ƒํ•˜์ขŒ์šฐ ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋Š” bfsํ•จ์ˆ˜์—์„œ๋Š” nx, ny์˜ ์œ„์น˜๋ฅผ nx, ny = x + dx, y + dy๋กœ ์žก์•„์ฃผ์—ˆ๋‹ค. 

 

๊ทธ๋Ÿฌ๋‚˜ ๋ฆฌ์ฝ”๋ › ๋กœ๋ด‡์€ ์žฅ์• ๋ฌผ์— ๋‹ฟ์„ ๋•Œ๊นŒ์ง€ ๋ฏธ๋„๋Ÿฌ์ง€๋“ฏ ์ด๋™ํ•ด์•ผ ํ•˜๋ฏ€๋กœ 

์žฅ์• ๋ฌผ์— ๋‹ฟ์„ ๋•Œ ๊นŒ์ง€ dx, dy ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. 

(dx, dy์˜ ๋ฐฉํ–ฅ์„ ๋”ฐ๋ผ ์žฅ์• ๋ฌผ(๋ฒ”์œ„ ๋ฒ—์–ด๋‚จ, D)์„ ๋งŒ๋‚˜๊ธฐ ์ „๊นŒ์ง€ ์ด๋™ํ•˜๋Š” ์ฝ”๋“œ)


โœ…์ •๋‹ต ์ฝ”๋“œ (Python)

from collections import deque
def solution(board):
    n = len(board)
    m = len(board[0])
    
    dxs, dys = [0, 0, 1, -1], [-1, 1, 0, 0]
    
    # ๋กœ๋ด‡ ์ดˆ๊ธฐ ์œ„์น˜ ๋“ฑ๋ก
    for i in range(n):    
        for j in range(m):
            if board[i][j] == 'R':
                rx, ry = i, j
    
    # bfs ์‚ฌ์ „ ์ค€๋น„
    q = deque()
    q.append((rx, ry))
    visited = [[0 for _ in range(m)] for _ in range(n)]
    visited[rx][ry] = 1
    
    # ๋ฒ”์œ„ ์ฒดํฌ ํ•จ์ˆ˜
    def in_range(x, y):
        if x < 0 or x >= n or y < 0 or y >= m:
            return False
        return True
    
    # ์ด๋™ ํ•จ์ˆ˜ - ํ•œ ๋ฒˆ์— ๋ฏธ๋Œ์–ด์ง€๋“ฏ ์ด๋™
    def move(x, y, dx, dy):
        nx, ny = x + dx, y + dy
        while in_range(nx, ny) and board[nx][ny] != 'D':
            nx += dx
            ny += dy
            
        return nx - dx, ny - dy
    
    def bfs():
        while q:
            x, y = q.popleft()
            for dx, dy in zip(dxs, dys):
                nx, ny = move(x, y, dx, dy) # nx, ny = x + dx, y + dy๋ฅผ ๋ณ€๊ฒฝ
                if in_range(nx, ny) and not visited[nx][ny] and board[nx][ny] != 'D':
                    q.append((nx, ny))
                    visited[nx][ny] = visited[x][y] + 1
                    
                    # ์ข…๋ฃŒ ์กฐ๊ฑด
                    if board[nx][ny] == 'G':
                        return visited[x][y]
        
        # G ๋งŒ๋‚˜์ง€ ๋ชปํ•˜๋ฉด -1 ๋ฆฌํ„ด
        return -1
    
    ans = bfs()
    
    return ans

๐Ÿ”ถ ์œ ์‚ฌ ๋ฌธ์ œ

๋ฐฑ์ค€_13460 ๊ตฌ์Šฌ ํƒˆ์ถœ 2

13460๋ฒˆ: ๊ตฌ์Šฌ ํƒˆ์ถœ 2 (acmicpc.net)

 

๋ฐฑ์ค€_7562 ๋‚˜์ดํŠธ์˜ ์ด๋™

7562๋ฒˆ: ๋‚˜์ดํŠธ์˜ ์ด๋™ (acmicpc.net)

๋ฐ˜์‘ํ˜•