๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 17086 - ์•„๊ธฐ ์ƒ์–ด2

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 17086๋ฒˆ

ํ’€์ด ๊ณผ์ •

์•„๊ธฐ ์ƒ์–ด์— ๋„๋‹ฌํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฑฐ๋ฆฌ๋ฅผ ์•ˆ์ „ ๊ฑฐ๋ฆฌ ๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
์ด ์•ˆ์ „ ๊ฑฐ๋ฆฌ ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ฐพ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์–ด๊ฐ€ ์—†๋Š” ๊ฐ ์นธ๋งˆ๋‹ค BFS ๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ ์•ˆ์ „ ๊ฑฐ๋ฆฌ ๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque

N, M = list(map(int, sys.stdin.readline().split()))
board = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]


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


def bfs(x, y):
    visit = [[0]*M for _ in range(N)]
    q = deque()

    visit[x][y] = 1
    q.append([x, y, 0])

    while q:
        x, y, cost = q.popleft()
        if board[x][y]:
            return cost

        for i in range(8):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < N and 0 <= ny < M:
                if not visit[nx][ny]:
                    visit[nx][ny] = 1
                    q.append([nx, ny, cost + 1])


def solution():
    answer = 0

    for x in range(N):
        for y in range(M):
            if not board[x][y]:
                answer = max(answer, bfs(x, y))

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 2688 - ์ค„์–ด๋“ค์ง€ ์•Š์•„  (0) 2021.03.14
BOJ 16472 - ๊ณ ๋ƒฅ์ด  (0) 2021.03.14
BOJ 13335 - ํŠธ๋Ÿญ  (0) 2021.03.08
BOJ 6497 - ์ „๋ ฅ๋‚œ  (0) 2021.03.08
BOJ 6156 - Cow Contest  (0) 2021.03.08

๐Ÿ’ฌ ๋Œ“๊ธ€