2차원 배열을 사용하여 구현하였습니다.
벽 부수고 이동하기 2 : https://seyeon-hello.tistory.com/62
import sys
from collections import deque
input=sys.stdin.readline
N,M,K=map(int,input().split())
arr=[list(map(int,input().strip())) for _ in range(N)]
dirs=[(1,0),(0,1),(-1,0),(0,-1)]
q=deque()
q.append((0,0,0,1,1)) #가로,세로,벽,이동,낮
visit=[[sys.maxsize]*M for _ in range(N)]
visit[0][0]=0
def bfs():
while q:
r,c,cnt,move,day=q.popleft()
if r==N-1 and c==M-1:
return move
for i in range(4):
nr,nc=r+dirs[i][0],c+dirs[i][1]
if 0>nr or nr>=N or nc<0 or nc>=M: continue
if visit[nr][nc]<cnt+1: continue
if arr[nr][nc]:
if cnt<K:
if day==1:
visit[nr][nc]=cnt+1
q.append((nr,nc,cnt+1,move+1, -day))
else:
q.append((r, c, cnt, move+1, -day))
else:
visit[nr][nc] = cnt
q.append((nr, nc, cnt, move + 1, -day))
return -1
print(bfs())
'IT > coding study' 카테고리의 다른 글
[programmers] COS Pro 1급 Python 모의고사 메모장(python) (0) | 2024.04.21 |
---|---|
[acmicpc] 14442. 벽 부수고 이동하기 2(python) (0) | 2023.08.12 |
[acmicpc] 5427. 불(python) (0) | 2023.03.31 |
[acmicpc] 13913. 숨바꼭질 4(python) (0) | 2023.03.28 |
[acmicpc] 16236. 아기상어(python) (0) | 2023.03.19 |