https://programmers.co.kr/learn/courses/30/lessons/81302#fn1
def solution(places):
answer = []
for p in places:
if close(p):
answer.append(0)
else:
answer.append(1)
return answer
def close(p):
for y in range(5):
for x in range(5):
if p[y][x] == "P":
if bfs(y, x, p):
return True
return False
from collections import deque
def bfs(y, x, board):
dy = [0,0,-1,1]
dx = [1,-1,0,0]
visited = [[0]*5 for _ in range(5)]
visited[y][x] = 1
q = deque()
q.append([y,x])
while q:
cy, cx = q.popleft()
for d in range(4):
ny, nx = cy+dy[d], cx+dx[d]
if 0<=ny<5 and 0<=nx<5:
if not visited[ny][nx]:
if visited[cy][cx] <= 2:
if board[ny][nx] == "P":
return True
if board[ny][nx] == "O":
visited[ny][nx] = visited[cy][cx] + 1
q.append([ny,nx])
if board[ny][nx] == "X":
pass
else:
pass
return False