๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 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 |
๐ฌ ๋๊ธ