Notice
Recent Posts
Recent Comments
Link
취준생의 코딩테스트 연습기
[프로그래머스] 게임 맵 최단거리 / 파이썬(python) 본문
# 문제 링크
https://programmers.co.kr/learn/courses/30/lessons/1844
코딩테스트 연습 - 게임 맵 최단거리
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]] 11 [[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,0],[0,0,0,0,1]] -1
programmers.co.kr
# Soultion
최단거리를 구하는 문제이므로 BFS를 이용하여 문제 해결.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
from collections import deque
def solution(maps):
answer = 0 #0벽, 1길
queue = deque([[0,0]])
n = len(maps)
m = len(maps[0])
d = [(1,0),(-1,0),(0,1),(0,-1)] #방향
while queue:
x,y = queue.popleft()
for dx,dy in d:
xx = x+dx
yy = y+dy
if 0<=xx<n and 0<=yy<m and maps[xx][yy]==1:
queue.append([xx,yy])
maps[xx][yy] = maps[x][y]+1
if maps[n-1][m-1]!=1:
return maps[n-1][m-1]
else:
return -1
|
cs |
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
| [프로그래머스] 정수 삼각형 / 파이썬(python) (0) | 2021.05.27 |
|---|---|
| [프로그래머스] 전화번호 목록 / 파이썬(python) (0) | 2021.05.27 |
| [백준] 1303번 전쟁-전투 / 파이썬(python) (0) | 2021.05.25 |
| [프로그래머스] [1차] 뉴스 클러스터링 / 파이썬(python) (0) | 2021.05.24 |
| [프로그래머스] 가장 먼 노드 / 파이썬(python) (0) | 2021.05.24 |
Comments