Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- @FeignClient
- 빈 조회 2개 이상
- infcon 2024
- 프로그래머스 이중우선순위큐
- DesignPattern
- 취업리부트코스
- 인프콘 2024
- 빈 충돌
- 디자인 패턴
- KPT회고
- jwt
- 전략패턴 #StrategyPattern #디자인패턴
- JavaScript
- 프로그래머스
- 커스텀 헤더
- TiL
- 항해99
- jwttoken
- 단기개발자코스
- 개발자 취업
- 디자인패턴
- 구글 OAuth login
- 코딩테스트 준비
- 개발자부트캠프추천
- 파이썬
- 1주일회고
- spring batch 5.0
- 99클럽
- Python
- Spring multimodule
Archives
- Today
- Total
m1ndy5's coding blog
99클럽 코테 스터디 12일차 TIL bfs/dfs : 프로그래머스 LV.2 게임 맵 최단거리 with Python 본문
알고리즘 with python/알고리즘 스터디
99클럽 코테 스터디 12일차 TIL bfs/dfs : 프로그래머스 LV.2 게임 맵 최단거리 with Python
정민됴 2024. 5. 31. 13:07https://school.programmers.co.kr/learn/courses/30/lessons/1844
문제는 위 쪽에서!
전형적인 최단거리 길찾기 문제!
from collections import deque
def solution(maps):
answer = -1
# 하, 상, 좌, 우
dx, dy = (0, 0, -1, 1), (-1, 1, 0, 0)
w, h = len(maps[0]), len(maps)
visited = [[0 for _ in range(w)] for _ in range(h)]
visited[0][0] = 1
q = deque([[0, 0, 1]])
while q:
y, x, step = q.popleft()
if y == h-1 and x == w-1:
answer = step
break
for i in range(4):
if 0 <= y+dy[i] < h and 0 <= x+dx[i] < w and maps[y+dy[i]][x+dx[i]] == 1:
if visited[y+dy[i]][x+dx[i]] == 0:
q.append([y+dy[i], x+dx[i], step+1])
visited[y+dy[i]][x+dx[i]] = 1
return answer
bfs를 사용해서 풀었다.
방문한 곳을 visited 처리하면서 가장 먼저 목적지에 도착하는 방법을 리턴!! 하는 방식이다.
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
99클럽 코테 스터디 11일차 TIL bfs/dfs : 프로그래머스 LV.2 타겟넘버 with Python (0) | 2024.05.30 |
---|---|
99클럽 코테 스터디 10일차 TIL 완전탐색 : 프로그래머스 LV.2 소수 찾기 with Python (0) | 2024.05.29 |
99클럽 코테 스터디 9일차 TIL 완전탐색 : 프로그래머스 LV.2 카펫 with Python (0) | 2024.05.28 |
99클럽 코테 스터디 8일차 TIL 정렬 : 프로그래머스 LV.2 H-Index with Python (0) | 2024.05.27 |
99클럽 코테 스터디 7일차 TIL 정렬 :프로그래머스 LV.2 가장 큰 수 with Python (0) | 2024.05.26 |