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 |
29 | 30 |
Tags
- 백준 파이썬
- 알고리즘 조건
- IPv4 주소체계
- string유형
- 유형 변환
- 문자형 배열
- 네트워크 결합
- 주기억장치
- getline()함수
- 원형 연결 구조 연결된 큐
- r-value참조자
- 프로그래머스 배열만들기4
- l-value참조자
- 괄호 검사 프로그램
- const l-value참조자
- 프로그래머스 푸드 파이트 대회
- auto 키워드
- 운영체제 기능
- C언어 덱
- const화
- LAN의 분류
- C언어 스택 연산
- 입출력 관리자
- 범위 기반 for문
- 논리 연산
- 문제해결 단계
- 값/참조/주소에 의한 전달
- c언어 괄호검사
- C언어 계산기 프로그램
- 회전 및 자리 이동 연산
Archives
- Today
- Total
chyam
[프로그래머스 Lv2, python] - 미로 탈출 본문

https://school.programmers.co.kr/learn/courses/30/lessons/159993
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
from collections import deque
def startPosition(maps,target): # 시작 위치 찾기
pos=[]
for i in range(len(maps)):
for j in range(len(maps[0])):
if maps[i][j] == target:
pos = (i,j)
break
if len(pos)!=0:
break
return pos
def bfs(maps,pos,target):
cnt=0 # 방문 거리
m, n=len(maps), len(maps[0]) # m행 n열
visited = [[0]*n for _ in range(m)]
queue = deque([(pos[0], pos[1], cnt)]) # 시작위치
visited[pos[0]][pos[1]]=1 # 방문함
direct=[(0,1),(1,0),(0,-1),(-1,0)] # 방향 저장
while queue:
v = queue.popleft()
x, y, cnt = v[0], v[1], v[2]
if maps[x][y] == target:
return cnt
for dx, dy in direct:
if 0 <= x + dx < len(maps) and 0 <= y + dy < len(maps[0]) and visited[x + dx][y + dy]==0 and maps[x+dx][y+dy] != "X":
queue.append((x + dx, y + dy, cnt + 1))
visited[x + dx][y + dy] = 1
return -1
def solution(maps):
start = startPosition(maps,"S") # S 위치
lever = startPosition(maps,"L") # L 위치
cnt_L = bfs(maps,start,"L") # S -> L 거리
if cnt_L == -1:
return -1
cnt_E = bfs(maps,lever,"E") # L -> E 거리
if cnt_E == -1:
return -1
return cnt_L+cnt_E
'프로그래머스 > LV2' 카테고리의 다른 글
[프로그래머스 Lv2, python] - [3차] 방금그곡 (0) | 2025.03.07 |
---|---|
[프로그래머스 Lv2, python] - 배달 (0) | 2025.03.06 |
[프로그래머스 Lv2, python] - 서버 증설 횟수 (1) | 2025.03.04 |
[프로그래머스 Lv2, python] - 마법의 엘리베이터 (0) | 2025.03.03 |
[프로그래머스 Lv2, python] - 숫자 카드 나누기 (0) | 2025.02.27 |