๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ๋ณด๊ธ‰๋กœ SWEA #1249

by seolhee2750 2022. 7. 5.
๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import math
from collections import deque

tc = int(input())
INF = math.inf

for t in range(tc):
    n = int(input())
    mapArr = [list(map(int, list(input()))) for _ in range(n)]
    visited = [[INF for _ in range(n)] for _ in range(n)]
    dx = [0, 0, -1, 1]
    dy = [-1, 1, 0, 0]

    def bfs(deq, visited):
        while deq:
            (x, y, sums) = deq.popleft()

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

                if nx < 0 or ny < 0 or nx >= n or ny >= n or (nx, ny) == (0, 0) or visited[nx][ny] <= mapArr[nx][ny] + sums:
                    continue

                visited[nx][ny] = mapArr[nx][ny] + sums
                deq.append((nx, ny, visited[nx][ny]))
        return visited[-1][-1]

    deq = deque()
    deq.append((0, 1, mapArr[0][1]))
    deq.append((1, 0, mapArr[1][0]))
    print("#" + str(t + 1) + " " + str(bfs(deq, visited)))

๐Ÿ‘‰ BFS, ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ !

 

[๋ณ€์ˆ˜ ์„ค๋ช…]

  • mapArr๋Š” ์ž…๋ ฅ๋˜๋Š” ์ง€๋„ ๊ฐ’์„ intํ˜• 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•„์ค€๋‹ค.
  • visited๋Š” bfs ํ•จ์ˆ˜๋ฅผ ๋Œ๋ฆด ๋•Œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•ด์ฃผ๋Š” ๋™์‹œ์—,
    ํ•ด๋‹น ์ž๋ฆฌ๊นŒ์ง€ ์˜ค๋Š”๋ฐ ๊ฑธ๋ฆฐ ์ตœ์†Ÿ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
    => ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ INF๋Š” ์•„~์ฃผ ํฐ ๊ฐ’์ด๋ฏ€๋กœ, ์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹ ์— ์œ ์šฉํ•˜๋‹ค.
  • dx, dy๋Š” bfs ํ•จ์ˆ˜์—์„œ ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ด๋Š” ๊ฒƒ์„ ๋•๋Š”๋‹ค.

[ํ’€์ด ์„ค๋ช…]

  • bfsํ•จ์ˆ˜์—์„œ ํƒ์ƒ‰ํ•  ์ž๋ฆฌ๋“ค์„ ์ €์žฅํ•ด์ค„ ์žฅ์†Œ๋ฅผ deq์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ๋จผ์ € ํƒ์ƒ‰์ด ์‹œ์ž‘๋˜์–ด์•ผ ํ•  [0][1], [1][0]์— ๋Œ€ํ•œ ์ขŒํ‘œ๊ฐ’๊ณผ,
    ๊ทธ ์ž๋ฆฌ๊นŒ์ง€ ์˜ค๋Š”๋ฐ ๊ฑธ๋ฆฐ ์ตœ์†Ÿ๊ฐ’์„ ํ•จ๊ป˜ ํŠœํ”Œ ํ˜•ํƒœ๋กœ ์ €์žฅํ–ˆ๋‹ค.
  • deq์— ์ €์žฅ๋œ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜๋Š”๋ฐ,
    ๊ทธ ๊ฐ’์„ x, y, sums์— ์ฐจ๋ก€๋Œ€๋กœ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•ด์ฃผ์—ˆ๋‹ค.
  • ์•ž์„œ ์ •์˜ํ•œ dx, dy์™€ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•ด์ฃผ๋Š”๋ฐ,
    1) ํƒ์ƒ‰ํ•  ์ž๋ฆฌ๊ฐ€ ์ง€๋„์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜
    2) ์‹œ์ž‘์ง€์ ์ด๊ฑฐ๋‚˜
    3) ์ด๋ฏธ visited[nx][ny]์— ์ €์žฅ๋œ ๊ฐ’์ด ์ถฉ๋ถ„ํžˆ ์ž‘์€ ๊ฒฝ์šฐ
    ์ด๋ ‡๊ฒŒ ์„ธ ๊ฒฝ์šฐ์—๋Š” ๋”์ด์ƒ ํƒ์ƒ‰ํ•  ์ˆ˜ ์—†๊ฑฐ๋‚˜, ํƒ์ƒ‰ํ•  ์ด์œ ๊ฐ€ ์—†์œผ๋ฏ€๋กœ continue ํ•ด์ค€๋‹ค.
  • ์œ„์˜ 3)๋ฒˆ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด์„œ๋งŒ ์ถ”๊ฐ€๋กœ ์„ค๋ช…ํ•ด๋ณด์ž๋ฉด
    visited[nx][ny]์— INF๊ฐ€ ์•„๋‹Œ ๊ฐ’์ด ์ €์žฅ๋˜์–ด์žˆ๋‹ค๋Š” ๊ฒƒ์€ ์ด๋ฏธ ํ•œ ๋ฒˆ ๋ฐฉ๋ฌธํ•œ ์  ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๊ณ ,
    ๊ทธ ๊ฐ’์ด ํƒ์ƒ‰ํ•˜๋ ค๋Š” ์ž๋ฆฌ mapArr[nx][ny]์˜ ๊ฐ’๊ณผ, ํ˜„์žฌ mapArr[x][y]๊นŒ์ง€ ์˜ค๋Š”๋ฐ ๊ฑธ๋ฆฐ ๊ฐ’๋ณด๋‹ค
    ๋” ์ž‘๋‹ค๋ฉด, ์ด๋ฏธ ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ๊ฒฝ๋กœ๋Š” ์ตœ์†Œ๊ฐ€ ์•„๋‹˜์ด ํŒ๋ช…๋˜๋ฏ€๋กœ ์œ„์™€๊ฐ™์€ ์˜ˆ์™ธ ์ƒํ™ฉ์„ ๋งŒ๋“ค์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์˜ˆ์™ธ์— ๊ฑธ๋ฆฌ์ง€ ์•Š์•˜๋‹ค๋ฉด visited[nx][ny]๊ฐ€ ๋ฐฉ๋ฌธ๋œ ์  ์žˆ๋”๋ผ๋„
    ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ์ตœ์†Œ๋ผ๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ visited ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ์—ˆ๊ณ ,
    deq์—๋Š” ๋˜ ์ƒˆ๋กœ์šด ํƒ์ƒ‰ ํ•„์š” ์ขŒํ‘œ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๋ฐ˜๋ณต์ด ์ง„ํ–‰๋  ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰์—๋Š” visited์˜ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ์— ์ €์žฅ๋œ ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๋„์ฐฉ์ ์— ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ์†Ÿ๊ฐ’์„ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ฒ˜์Œ์—” deque๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  idx ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ˆ˜๋™์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ์˜ฎ๊ฒจ๊ฐ€๋ฉฐ
    while ๋ฐ˜๋ณต์ด ์ง„ํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ์ปค์ง€๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.
    ๋”ฐ๋ผ์„œ deque๋กœ ํ’€์ดํ–ˆ๋”๋‹ˆ ๋ฐ”๋กœ ํ†ต๊ณผ,,!
  • ์ด์ „๊นŒ์ง€๋Š” ๋ญ”๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์‹ซ์–ด์„œ
    deque๋Œ€์‹  ์ธ๋ฑ์Šค ๊ฐ’์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฃผ๋กœ ์ผ์—ˆ๋Š”๋ฐ,
    ์•ž์œผ๋กœ๋Š” deque๋„ ์• ์šฉํ•ด๋ด์•ผ๊ฒ ๋‹ค. ๋” ์ž์„ธํžˆ ๊ณต๋ถ€๋„ ํ•ด๋ณด๊ณ  !

๋Œ“๊ธ€