반응형
문제
간단한 BFS 문제인데, 오랜만에 BFS를 풀어서 버벅거렸다.
그래도 나름 금방 풀었다.
코드
def solution(maps):
global mX,mY
mX = [0,1,0,-1]
mY = [1,0,-1,0]
return bfs(maps)
def bfs(maps):
endX = len(maps) - 1
endY = len(maps[0]) - 1
isVisit = [[ False for j in range(len(maps[0]))] for i in range(len(maps))]
queue = [Point(0,0,1)]
isVisit[0][0] = True
while len(queue) != 0:
p = queue.pop(0)
if p.x == endX and p.y == endY:
return p.cnt
for i in range(4):
nX, nY = p.x + mX[i], p.y + mY[i]
if nX < 0 or nY < 0 or nX > endX or nY > endY or isVisit[nX][nY] == True or maps[nX][nY] == 0:
continue
isVisit[nX][nY] = True
queue.append(Point(nX,nY,p.cnt+1))
return -1
class Point:
def __init__(self,x,y,cnt):
self.x = x
self.y = y
self.cnt = cnt
반응형
'👨🏫ps > ❄️프로그래머스' 카테고리의 다른 글
[프로그래머스] 숫자 변환하기 [python] (0) | 2023.03.24 |
---|---|
[프로그래머스] 큰 수 만들기 [python] (0) | 2023.03.19 |
[프로그래머스] 숫자의 표현 [python] (0) | 2023.03.15 |
[프로그래머스] 신규 아이디 추천 [python] (0) | 2023.03.14 |
[프로그래머스] 성격 유형 검사하기 [python] (0) | 2023.03.14 |