[BOJ] #18405 ๊ฒฝ์Ÿ์  ์ „์—ผ (Python)

    ๋ฐ˜์‘ํ˜•

    ๐ŸŒฑ ๋ฌธ์ œ

     

    18405๋ฒˆ: ๊ฒฝ์Ÿ์  ์ „์—ผ

    ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N, K๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 200, 1 ≤ K ≤ 1,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์‹œํ—˜๊ด€์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ–‰์€ N๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ํ•ด๋‹น ์œ„์น˜

    www.acmicpc.net

    https://www.acmicpc.net/problem/18405

     

    ๐ŸŒฑ ํ’€์ด

    0. ์ž…๋ ฅ ๋ฐ›๊ธฐ

    n, k = map(int, input().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int, input().split())))
    s, x, y = map(int, input().split())
    • ์ฒซ์งธ์ค„์— ์‹œํ—˜๊ด€์˜ ํฌ๊ธฐ(n)์™€ ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ข…๋ฅ˜์˜ ๊ฐœ์ˆ˜(k)๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.
    • ๋‹ค์Œ n๊ฐœ์— ์ค„์— ์‹œํ—˜๊ด€์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.
    • ๋งˆ์ง€๋ง‰ ์ค„์— ์‹œ๊ฐ„(s), ์ถœ๋ ฅํ•  ์‹œํ—˜๊ด€ ์นธ์˜ ์ •๋ณด(x, y)๋ฅผ ์ž…๋ ฅ ๋ฐ›์Šต๋‹ˆ๋‹ค.

     

    1. ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ ์„ค์ •

    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]

    ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์ƒํ•˜์ขŒ์šฐ๋กœ ์ฆ์‹ํ•˜๋ฏ€๋กœ ์ƒ, ํ•˜, ์ขŒ, ์šฐ ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

     

    2. ๋งต์„ ์™„์ „ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ฐ”์ด๋Ÿฌ์Šค ์œ„์น˜ ์ •๋ณด ์ €์žฅํ•˜๊ธฐ

    queue = deque([])
    for i in range(n):
        for j in range(n):
            if graph[i][j] != 0:
                queue.append((i, j))

    ๋งจ ์ฒ˜์Œ queue์—๋Š” ์ž…๋ ฅ๋ฐ›์€ ์‹œํ—˜๊ด€ ์ •๋ณด์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ์œ„์น˜๋ฅผ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

     

    3. ๋งค ์ดˆ๋งˆ๋‹ค ๋ฐ”์ด๋Ÿฌ์Šค ํ™•์‚ฐ์‹œํ‚ค๊ธฐ

    for sec in range(s):
    
        # ๋งค ์ดˆ์— ํ™•์‚ฐ๋œ ๋ฐ”์ด๋Ÿฌ์Šค ์œ„์น˜ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
        spread = []
        while queue:
            i, j = queue.popleft()
    
            for k in range(4):
                nx = i + dx[k]
                ny = j + dy[k]
    
                if nx < 0 or ny < 0 or nx > n - 1 or ny > n - 1:
                    continue
    
                if graph[nx][ny] == 0 or (graph[nx][ny] > graph[i][j] and (nx, ny) in spread):
                    spread.append((nx, ny))
                    graph[nx][ny] = graph[i][j]
    
        for l in spread:
            queue.append(l)
    • ์ด ๋ฌธ์ œ๋Š” ๊ฐ€๋Šฅ ๊ณณ๊นŒ์ง€ ํ•œ ๋ฒˆ์— ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋งค ์ดˆ๋งˆ๋‹ค ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ƒํ•˜์ขŒ์šฐ๋กœ๋งŒ ํ™•์‚ฐ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•œ ๊ณณ๊นŒ์ง€ ํ•œ๋ฒˆ์— ํƒ์ƒ‰ํ•˜์ง€ ์•Š๊ณ  ์ฃผ์–ด์ง„ ์ดˆ(s)๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค queue๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
    • spread ๋ฆฌ์ŠคํŠธ๋Š” ๋งค ์ดˆ์— ํ™•์‚ฐ๋œ ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜๋งŒ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. queue์— ์žˆ๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๋นผ๋‚ด๊ฐ€๋ฉฐ ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰ํ›„, spread์— ๋„ฃ๊ณ , queue์— ์žˆ๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๋ชจ๋‘ ๊บผ๋‚ด์—ˆ์„ ๋•Œ, queue์— spread์— ์ €์žฅํ•ด๋†“์€ ์œ„์น˜๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
    • ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํ™•์‚ฐ๋˜๋Š” ๊ฒฝ์šฐ(spread์— ๋„ฃ์–ด์ง€๋Š” ๊ฒฝ์šฐ)๋Š” ๋‹ค์Œ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
      • ํ™•์‚ฐ๋  ์œ„์น˜์— ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ๊ฒฝ์šฐ(graph[nx][ny]๊ฐ€ 0์ธ ๊ฒฝ์šฐ)
      • ํ™•์‚ฐ๋  ์œ„์น˜์— ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ๋ฐ”์ด๋Ÿฌ์Šค(graph[i][j])๋ณด๋‹ค ํ˜„์žฌ ์‹œ๊ฐ„์— ํ™•์‚ฐ๋œ(spread์— ์žˆ๋Š”) ๋” ํฐ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ (graph[nx][ny] > graph[i][j] and (nx, ny) in spread์ธ ๊ฒฝ์šฐ)
        (๋งค ์ดˆ๋งˆ๋‹ค ๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ์ข…๋ฅ˜์˜ ๋ฐ”์ด๋Ÿฌ์Šค๋ถ€ํ„ฐ ๋จผ์ € ์ฆ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ)

     

    4. ์ถœ๋ ฅํ•˜๊ธฐ

    print(graph[x - 1][y - 1])

     

    ๐ŸŒฑ ์ฝ”๋“œ

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    # boj 18405 ๊ฒฝ์Ÿ์  ์ „์—ผ
    
    import sys
    from collections import deque
    
    input = sys.stdin.readline
    
    n, k = map(int, input().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int, input().split())))
    s, x, y = map(int, input().split())
    
    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]
    
    queue = deque([])
    for i in range(n):
        for j in range(n):
            if graph[i][j] != 0:
                queue.append((i, j))
    
    for sec in range(s):
    
        # ๋งค ์ดˆ์— ํ™•์‚ฐ๋œ ๋ฐ”์ด๋Ÿฌ์Šค ์œ„์น˜ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
        spread = []
        while queue:
            i, j = queue.popleft()
    
            for k in range(4):
                nx = i + dx[k]
                ny = j + dy[k]
    
                if nx < 0 or ny < 0 or nx > n - 1 or ny > n - 1:
                    continue
    
                if graph[nx][ny] == 0 or (graph[nx][ny] > graph[i][j] and (nx, ny) in spread):
                    spread.append((nx, ny))
                    graph[nx][ny] = graph[i][j]
    
        for l in spread:
            queue.append(l)
    
    print(graph[x - 1][y - 1])
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€