๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/๋ฐฑ์ค€

[๋ฐฑ์ค€] #18405 ๊ฒฝ์Ÿ์  ์ „์—ผ (Python)

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • ์ž…๋ ฅ
    • ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N, K๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง
    • ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์‹œํ—˜๊ด€์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง : ์กด์žฌํ•˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ฃผ์–ด์ง, ๋‹จ, ํ•ด๋‹น ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ 0์ด ์ฃผ์–ด์ง ( ๋ชจ๋“  ๋ฐ”์ด๋Ÿฌ์Šค์˜ ๋ฒˆํ˜ธ๋Š” K์ดํ•˜์˜ ์ž์—ฐ์ˆ˜)
    • N+2๋ฒˆ์งธ ์ค„์—๋Š” S, X, Y๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง
  • ์ถœ๋ ฅ : S์ดˆ ๋’ค์— (X,Y)์— ์กด์žฌํ•˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ข…๋ฅ˜๋ฅผ ์ถœ๋ ฅ, ๋งŒ์•ฝ S์ดˆ ๋’ค์— ํ•ด๋‹น ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, 0์„ ์ถœ๋ ฅ

2. ์ฝ”๋“œ

solution1.py

from collections import deque
import sys
input = sys.stdin.readline

N,K = map(int, input().split())
graph = []
data = [] # ๋ฐ”์ด๋Ÿฌ์Šค์ข…๋ฅ˜ ์ €์žฅ
for i in range(N):
    graph.append(list(map(int, input().split())))
    for j in range(N):
        #ํ•ด๋‹น ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ
        if graph[i][j] !=0:
            #๋ฐ”์ด๋Ÿฌ์Šค์ข…๋ฅ˜, ์ดˆ, ์œ„์น˜ x, ์œ„์น˜ y
            data.append((graph[i][j],0,i,j))

data.sort()

#ex1 : deque([(1, 0, 0, 0), (2, 0, 0, 2), (3, 0, 2, 0)])
q = deque(data)

target_s, target_x, target_y = map(int, input().split()) #์ดˆ, ์œ„์น˜ x, ์œ„์น˜ y

#์ƒํ•˜์ขŒ์šฐ
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]

while q:
    virus, s, x, y = q.popleft()
    #s์ดˆ๊ฐ€ ์ง€๋‚˜๊ฑฐ๋‚˜, ํ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    if s == target_s:
        break
    #ํ˜„์žฌ ๋…ธ๋“œ์—์„œ 4๊ฐ€์ง€ ์œ„์น˜๋ฅผ ๊ฐ๊ฐ ํ™•์ธ
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        #ํ•ด๋‹น ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ
        if 0 <= nx and nx < N and 0 <=ny  and ny < N:
            #์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์œ„์น˜๋ผ๋ฉด, ๊ทธ ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค ๋„ฃ๊ธฐ
            if graph[nx][ny] == 0:
                graph[nx][ny] = virus
                q.append((virus, s+1, nx,ny))

print(graph[target_x -1][target_y -1])

3. ํšŒ๊ณ 

Q. ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋“  ์ƒ๊ฐ

  • ์ œ๊ณต๋œ ์‹œํ—˜๊ด€ ์ •๋ณด๋ฅผ bfs๋ฅผ ์ ์šฉํ•˜๋ ค๋ฉด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ €์žฅํ•ด์•ผํ•˜์ง€? ์ธ์ ‘๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๊ธฐ์—” ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์€๋ฐ...
    • 2์ฐจ์› ๋ฆฌ์ŠคํŠธ์— ๊ทธ๋ƒฅ ๋‹ด์œผ๋ฉด ๋จ!
  • ๋ชจ๋“  ๊ฐ„์„ ์˜ ๋น„์šฉ์ด 1์ด๊ธฐ ๋•Œ๋ฌธ์— BFS๋ฅผ ํ†ตํ•œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅ
    • BFS๋กœ ํ’€๊ธฐ๋กœ ๊ฒฐ์ •

Q. ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด์„œ ๋“ค์—ˆ๋˜ ์˜๋ฌธ์ ๋“ค

  1. ์–ด๋–ป๊ฒŒ ์ž‘์€ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ„์น˜๋ฅผ ์„ ์ ์„ ํ–ˆ๋Š”์ง€?
  • ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋‚ฎ์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ์ฆ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ์— ๋‚ฎ์€ ๋ฒˆํ˜ธ์˜ ๋ฐ”์ด๋Ÿฌ์Šค๋ถ€ํ„ฐ ๋„ฃ์–ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ์ž๋™์œผ๋กœ BFS๋ฅผ ์ˆ˜ํ–‰ ์‹œ์— ๋‚ฎ์€ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ์ฆ์‹ํ•˜๋Š” ๋ฐฉ์‹์ด ์ ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.
  1. ํ์—์„œ ๊บผ๋‚ผ๋•Œ ์ˆœ์„œ๋Š”?
  • ์ฒ˜์Œ์—” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ๊ณณ ์œ„์น˜๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๊บผ๋ƒ„
  • ๊ทธ ํ›„์—๋Š” ์ƒˆ๋กญ๊ฒŒ ์—…๋ฐ์ดํŠธ ๋œ ๊ณณ์—์„œ์˜ (๋ฐ”์ด๋Ÿฌ์Šค, ์ดˆ,x,y)๋ฅผ ํ์— ๋„ฃ๊ณ , ์กด์žฌํ•˜๋˜ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•œ ํ›„์—๋Š” ์ƒˆ๋กญ๊ฒŒ ๋„ฃ์€ (๋ฐ”์ด๋Ÿฌ์Šค, ์ดˆ,x,y)๋ฅผ ๊บผ๋ƒ„
  1. ๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ณณ(0์ด ์•„๋‹Œ ๊ณณ)์€ ํ์— ๋„ฃ์–ด์ฃผ์ง€ ์•Š์•˜๋Š”๊ฐ€?
  • ??? (๋ฏธํ•ด๊ฒฐ)
  1. ์‹œ๊ฐ„์€ ์–ด๋–ป๊ฒŒ count?
  • ํ์— ์‹œ๊ฐ„์ •๋ณด๋ฅผ ์ €์žฅํ•ด๋‘๊ณ , ์ €์žฅํ•ด๋‘” ๊ฐ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์ดˆ๊ธฐ์‹œ๊ฐ„์ด 0, ์ƒˆ๋กญ๊ฒŒ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋…ธ๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด +1ํ•ด์„œ ์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ ํ›„, ํ์— ๋„ฃ์–ด์ค€๋‹ค.
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ์—์„œ ๊บผ๋ƒˆ์„ ๋•Œ, ํ•ด๋‹น ๋…ธ๋“œ๋Š” ์‹œ๊ฐ„์ด ์ฆ๊ฐ€ํ•œ ์ƒํƒœ, ์ข…๋ฅ˜์กฐ๊ฑด์„ ํ™•์ธํ•˜๋Š”๋ฐ ๋„์›€
  1. ์ข…๋ฃŒ์กฐ๊ฑด?
  • ํ์—์„œ ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ๊บผ๋ƒˆ๋Š”๋ฐ, ๊ทธ๊ฒŒ ๋งŒ์•ฝ S์ดˆ๋ผ๋ฉด ๊ฑฐ๊ธฐ๊นŒ์ง€๋งŒ ์ง„ํ–‰ํ•˜๊ณ  ๊ทธ ์ดํ›„ ์ง„ํ–‰์€ ๋ง‰์•„์•ผํ•˜๋ฏ€๋กœ, break
  • ๋งŒ์•ฝ ์ฃผ์–ด์ง„ S์ดˆ ์ „์— ํ ์•ˆ์— ๊ฐ’์ด ๋”์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ break, ์›ํ•˜๋Š” ์‹œ๊ฐ„ ์ด์ „์— ์ด๋ฏธ ๋ชจ๋“  ์‹œํ—˜๊ด€์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ „์—ผ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ ์ƒํƒœ์—์„œ ๊ฒฐ๊ณผ๋ฅผ ํƒ์ƒ‰

4. ์ฐธ๊ณ 

https://www.youtube.com/watch?v=PqzyFDUnbrY