peacekim
할 수 있는 것과 할 수 없는 것.
peacekim
전체 방문자
오늘
어제
  • 분류 전체보기 (68)
    • 👨‍🏫ps (44)
      • ❄️프로그래머스 (20)
      • 🔟0️⃣백준 (21)
      • leetcode (3)
    • ✍🏻study (20)
      • 👐java (6)
      • 🍃spring (1)
      • 🥇algorithm (0)
      • 🚘oodp (4)
      • 📒 jpa (3)
      • 👣DB (2)
      • 🌂네트워크 (0)
      • 🎸기타 (3)
      • 👊 kotlin (1)
      • 🫥 jvm (0)
    • 📽project (4)
      • 🎀ReBoN (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 글

티스토리

hELLO · Designed By 정상우.
peacekim

할 수 있는 것과 할 수 없는 것.

[프로그래머스] 게임 맵 최단거리
👨‍🏫ps/❄️프로그래머스

[프로그래머스] 게임 맵 최단거리

2023. 3. 16. 22:27
반응형

문제

간단한 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
    '👨‍🏫ps/❄️프로그래머스' 카테고리의 다른 글
    • [프로그래머스] 숫자 변환하기 [python]
    • [프로그래머스] 큰 수 만들기 [python]
    • [프로그래머스] 숫자의 표현 [python]
    • [프로그래머스] 신규 아이디 추천 [python]
    peacekim
    peacekim
    할 수 있는 것과 할 수 없는 것. github: https://github.com/PyeongGangKim

    티스토리툴바