https://www.acmicpc.net/problem/4179
import sys
from collections import deque
input = sys.stdin.readline
dx = [0, 0, 1, -1]
dy = [-1, 1, 0, 0]
# r:행, c:열
r, c = map(int, input().split())
que = deque()
maze = []
fire = []
visited = [[0 for _ in range(c)]for _ in range(r)]
for _ in range(r):
temp = list(input().rstrip('\n'))
maze.append(temp)
for i in range(r):
for j in range(c):
if maze[i][j] == 'J':
visited[i][j] = 1
que.append((i, j, 'J'))
elif maze[i][j] == 'F':
que.appendleft((i, j, 'F'))
def bfs():
while que:
x, y, s = que.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < r and 0 <= ny < c:
if visited[nx][ny] == 0 and maze[nx][ny] == '.':
visited[nx][ny] = visited[x][y] + 1
que.append((nx, ny, s))
else:
if s == 'J':
return visited[x][y]
ans = bfs()
if ans:
print(ans)
else:
print("IMPOSSIBLE")
que에 3가지 값(위치x, 위치y, 지훈/불)을 넣어주었다.
bfs를 돌리면서 (nx,ny,지훈/불) 값을 넣어주었다.
que를 하나만 쓰고 구현할 수 있다!