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

BOJ 13565 - ์นจํˆฌ

by HandHand 2021. 3. 5.

๋ฌธ์ œ

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

ํ’€์ด ๊ณผ์ •

์„ฌ์œ ์˜ ๋ฐ”๊นฅ์ชฝ์—์„œ ์•ˆ์ชฝ์œผ๋กœ ์นจํˆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋‹จํ•˜๋Š” BFS ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

0๋ฒˆ์งธ ํ–‰์—์„œ ์‹œ์ž‘ํ•ด์„œ M-1 ๋ฒˆ์งธ ํ–‰์— ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋‹จํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque

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

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


def in_range(x, y):
    return 0 <= x < M and 0 <= y < N


def bfs(visit, x, y):
    q = deque()

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

    while q:
        x, y = q.popleft()
        if x == M - 1:
            return True

        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            if in_range(nx, ny) and not visit[nx][ny] and board[nx][ny] == '0':
                visit[nx][ny] = 1
                q.append([nx, ny])

    return False


def solution():
    percolate = False
    visit = [[0] * N for _ in range(M)]

    for y in range(N):
        if not visit[0][y]:
            percolate = bfs(visit, 0, y)
            if percolate:
                break

    return 'YES' if percolate else 'NO'


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

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

BOJ 19941 - ํ–„๋ฒ„๊ฑฐ ๋ถ„๋ฐฐ  (0) 2021.03.08
BOJ 16469 - ์†Œ๋…„ ์ ํ”„  (0) 2021.03.05
BOJ 1105 - ํŒ”  (0) 2021.03.03
BOJ 16509 - ์žฅ๊ตฐ  (0) 2021.03.03
BOJ 3190 - ๋ฑ€  (0) 2021.03.01

๐Ÿ’ฌ ๋Œ“๊ธ€