일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 |
Tags
- 화상영어
- 개발자
- realclass
- 영어공부
- FIT XR
- 영어원서읽기
- 10분
- 운동
- 만화도
- 스탭퍼
- 쓰릴오브파이트
- Writing
- Problem Solving
- 매일
- 파비최
- 3줄정리
- English
- Daily Challenge
- 읽기
- 링피트
- leetcode
- 미드시청
- 30분
- 괜찮음
- 잡생각
- 리얼 클래스
- 뭐든
- 프로젝트
- 월간
- 사이드
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/
Nearest Exit from Entrance in Maze - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
BFS의 정석과 같은 문제
class Solution:
def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
rows, cols = len(maze), len(maze[0])
dirs = ((1, 0), (-1, 0), (0, 1), (0, -1))
start_row, start_col = entrance
maze[start_row][start_col] = "+"
qu = collections.deque()
qu.append([start_row, start_col, 0])
while qu:
curr_row, curr_col, curr_distance = qu.popleft()
for d in dirs:
next_row = curr_row + d[0]
next_col = curr_col + d[1]
if 0 <= next_row < rows and 0 <= next_col < cols and maze[next_row][next_col] == '.':
if 0 == next_row or next_row == rows - 1 or 0 == next_col or next_col == cols - 1:
return curr_distance + 1
maze[next_row][next_col] = '+'
qu.append([next_row, next_col, curr_distance + 1])
return -1
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.11.23 |
---|---|
Today's Challenge (0) | 2022.11.22 |
Today's Challenge (0) | 2022.11.20 |
Today's Challenge (0) | 2022.11.19 |
Today's Challenge (0) | 2022.11.18 |
Comments