[python]ํ๋ก๊ทธ๋๋จธ์ค - ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
https://school.programmers.co.kr/learn/courses/30/lessons/81302
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
from collections import deque
def bfs(arr):
start = [] #'P'์ธ ์ขํ๋ค ๋ชจ์ผ๊ธฐ
for i in range(5):
for j in range(5):
if arr[i][j] == 'P':
start.append([i,j])
#'P'๋ถํฐ ์์ํดbfs๋๋ฆฌ๊ธฐ
for s in start:
# print(s)
q = deque([s])
visit = [[0]*5 for _ in range(5)]
distance = [[0]*5 for _ in range(5)]
visit[s[0]][s[1]] = 1
# print(visit)
while q:
ny,nx = q.popleft()
directy = [-1,1,0,0]
directx = [0,0,-1,1]
for i in range(4):
dy = directy[i] + ny
dx = directx[i] + nx
if 0<=dy<5 and 0<=dx<5 and visit[dy][dx] == 0:
if arr[dy][dx] == 'O':
q.append([dy,dx])
visit[dy][dx] = 1
distance[dy][dx] = distance[ny][nx]+1
if arr[dy][dx] == 'P' and distance[ny][nx]<=1:
return 0
return 1
#๊ฐ ์ผ์ด์ค๋ง๋ค bfs๋ฅผ ๋๋ ค์ฃผ์
def solution(places):
answer = []
for p in places:
answer.append(bfs(p))
return answer
์์ ํ ์คํธ ์ผ์ด์ค๋ ์ฑ๊ณตํ๋๋ฐ, ์ ์ถํ๊ณ ๋์ ์คํจํ ์ผ์ด์ค๊ฐ ์์ด์ ํ์ฐธ๋์ ์ ํ๋ ธ์ง..? ํ๋๋ฐ
O๋ฅผ ์ซ์0์ผ๋ก ์ ์ด์์๋ค...ํํ
'Problem Solving > PROGRAMMERS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python]ํ๋ก๊ทธ๋๋จธ์ค - ๋ฐฉ๊ธ๊ทธ๊ณก (0) | 2022.08.24 |
---|---|
[python]ํ๋ก๊ทธ๋๋จธ์ค - ์คํจ์จ (0) | 2022.08.23 |
[python]ํ๋ก๊ทธ๋๋จธ์ค - ๋ด์ค ํด๋ฌ์คํฐ๋ง (0) | 2022.08.01 |
[python]ํ๋ก๊ทธ๋๋จธ์ค - ํํ (0) | 2022.08.01 |
[python]ํ๋ก๊ทธ๋๋จธ์ค - ์คํ์ฑํ ๋ฐฉ (0) | 2022.07.29 |
๋๊ธ