๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ(Lv.2) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 9. 5.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

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

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

programmers.co.kr

 

ํ’€์ด

์ด ์ฝ”๋“œ๋Š” ์ฃผ์–ด์ง„ maps ์—์„œ BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ฐœ์  (0, 0)์—์„œ ๋„์ฐฉ์  (m-1, n-1) ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

๊ฐ ์นธ์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋Š” 1๋กœ ํ‘œ์‹œ๋˜๋ฉฐ, 0์€ ๋ฒฝ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

 

1. ๋ณ€์ˆ˜ ์„ค๋ช… ๋ฐ ์ดˆ๊ธฐ ์„ค์ •

m = len(maps)       # ํ–‰์˜ ์ˆ˜
n = len(maps[0])    # ์—ด์˜ ์ˆ˜
  • m ์€ ๋งต์˜ ํ–‰(row) ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, n์€ ์—ด(column) ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ฆ‰, ๋งต์€ m x n ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค.
visited = [[False]*n for _ in range(m)]
  • visited๋Š” ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ธฐ๋กํ•˜๋Š” 2์ฐจ์› ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค.
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
  • dx ์™€ dy ๋Š” ์ƒํ•˜์ขŒ์šฐ ์ด๋™์„ ์œ„ํ•œ ๋ฐฉํ–ฅ์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ’๋“ค์„ ์ด์šฉํ•ด ๊ฐ๊ฐ ์œ„, ์•„๋ž˜, ์ขŒ, ์šฐ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

2. BFS ํ•จ์ˆ˜ ์„ค๋ช…

def bfs(x, y):
    q = deque()
    q.append([x, y])
    visited[x][y] = True
  • bfs ํ•จ์ˆ˜๋Š” (x, y) ์ขŒํ‘œ์—์„œ BFS ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  • deque๋Š” ํ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ, BFS๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ์‹œ์ž‘์  (0, 0)์„ ํ์— ๋„ฃ๊ณ  visited ๋ฅผ true๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
while q:
    xx, yy = q.popleft()
  • ํ๊ฐ€ ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๋™์•ˆ BFS๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. q.popleft() ๋Š” ํ์—์„œ ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด๊ฐ„ ์ขŒํ‘œ๋ฅผ ๊บผ๋ƒ…๋‹ˆ๋‹ค.
for i in range(4):
    nx = xx + dx[i]
    ny = yy + dy[i]
  • ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด dx, dy ๋ฐฉํ–ฅ์„ ์‚ฌ์šฉํ•ด ์ขŒํ‘œ (nx, ny)๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
if 0 <= nx < m and 0 <= ny < n:
    if maps[nx][ny] != 0 and not visited[nx][ny]:
        visited[nx][ny] = True
        maps[nx][ny] = maps[xx][yy] + 1
        q.append([nx, ny])
  • ์ƒˆ๋กœ์šด ์ขŒํ‘œ (ny, nx)๊ฐ€ ๋งต์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๊ณ  (0 <= nx < m and 0 <= ny < n), ๋ฒฝ์ด ์•„๋‹Œ ๊ณณ(maps[nx][ny] != 0) ์ด๋ฉฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ ( not visited[nx][ny] ) ์ธ ๊ฒฝ์šฐ์—๋งŒ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด visited[nx][ny] = True๋กœ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ธฐ๋กํ•˜๊ณ , ๊ทธ ์œ„์น˜์˜ ๊ฐ’์„ ํ˜„์žฌ ์œ„์น˜ maps[xx][yy]์— 1์„ ๋”ํ•œ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” BFS ํŠน์„ฑ์ƒ ์ตœ๋‹จ ๊ฒฝ๋กœ์ž„์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ดํ›„ (nx, ny) ์ขŒํ‘œ๋ฅผ ํ์— ์ถ”๊ฐ€ํ•˜์—ฌ ๊ณ„์† ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

 

3. ์ตœ์ข… ๊ฒฐ๊ณผ ๊ณ„์‚ฐ

if maps[m-1][n-1] == 1 or maps[m-1][n-1] == 0:
    answer = -1
else:
    answer = maps[m-1][n-1]

 

  • BFS ํƒ์ƒ‰์ด ๋๋‚œ ํ›„ ๋„์ฐฉ์  (m-1, n-1)์˜ ๊ฐ’์ด 1์ด๊ฑฐ๋‚˜ 0์ด๋ฉด, ์ด๋Š” ๊ฒฝ๋กœ๊ฐ€ ์—†์Œ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ answer = -1๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. 1์€ ๋„์ฐฉ์ ์ด ์ดˆ๊ธฐ ์ƒํƒœ์ธ ๊ฒฝ์šฐ์ด๋ฉฐ, 0์€ ๋„์ฐฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
  • ๋„์ฐฉ์ ์˜ ๊ฐ’์ด 1๋ณด๋‹ค ํฌ๋ฉด, ์ด๋Š” ์‹œ์ž‘์ ์—์„œ ๋„์ฐฉ์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฏ€๋กœ, ๊ทธ ๊ฐ’์„ answer์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

 

์ตœ์ข… ์ฝ”๋“œ

from collections import deque

def solution(maps):
    
    answer = 0
    m = len(maps)       # ํ–‰์˜ ์ˆ˜
    n = len(maps[0])    # ์—ด์˜ ์ˆ˜ 
    
    visited = [[False]*n for _ in range(m)]
    
    dx = [-1,1,0,0]
    dy = [0,0,-1,1]
    
    def bfs(x, y):
        q = deque()
        q.append([x,y])
        visited[x][y] = True
        
        while q:
            xx, yy = q.popleft()
            
            for i in range(4):
                nx = xx+dx[i]
                ny = yy+dy[i]
                
                if 0<= nx < m and 0 <= ny < n:
                    if maps[nx][ny] != 0 and not visited[nx][ny]:
                        visited[nx][ny] = True
                        maps[nx][ny] = maps[xx][yy] + 1
                        q.append([nx, ny])
        
    bfs(0,0)
    
    
    if maps[m-1][n-1] == 1 or maps[m-1][n-1] == 0:
        answer = -1
    else:
        answer = maps[m-1][n-1]
        
    return answer

 

728x90
๋ฐ˜์‘ํ˜•