728x90
4179번: 불!
입력의 첫째 줄에는 공백으로 구분된 두 정수 R과 C가 주어진다. 단, 1 ≤ R, C ≤ 1000 이다. R은 미로 행의 개수, C는 열의 개수이다. 다음 입력으로 R줄동안 각각의 미로 행이 주어진다. 각각의 문자
www.acmicpc.net
🤔 문제분석
불, 지훈 순으로 큐에 넣고 너비우선탐색을 하여 지훈이 그래프의 끝자락에 도착 할 수 있는 거리 + 1을 계산해주면된다. 만약, 불이 지훈이를 가로막는다면 지훈이는 갈 수 없다.
💻 코드
import sys
input = sys.stdin.readline
from collections import deque
R, C = map(int, input().split())
table = []
queue = deque()
ji_hun = []
for i in range(R):
temp = list(str(input().strip()))
for j in range(C):
if temp[j] == "F":
queue.append((i, j, 'F', 0))
elif temp[j] == "J":
ji_hun.append((i, j, 'J', 0))
table.append(temp)
def is_end(y, x):
return 0 == y or y == R-1 or 0 == x or x == C-1
is_arrived = False
for hun in ji_hun:
queue.append(hun)
while queue:
y, x, what, cost = queue.popleft()
if what == 'J' and is_end(y, x):
print(cost + 1)
is_arrived = True
break
for dy, dx in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
ny, nx = dy + y, dx + x
if R > ny >=0 and C > nx >=0 and table[ny][nx] == '.':
table[ny][nx] = what
queue.append((ny, nx, what, cost + 1))
if not is_arrived:
print("IMPOSSIBLE")
🎯 피드백 및 개선사항
이 문제는 이전에 풀었던 유형으로 쉽게 접근 할 수 있었습니다.
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 1865번 : 웜홀 (0) | 2024.03.10 |
---|---|
[백준] 1613번 : 역사 (0) | 2024.03.10 |
[백준] 11779번 : 최소비용 구하기2 (0) | 2024.03.05 |
[백준] 12851번 : 숨바꼭질 2 (1) | 2024.03.05 |
[백준] 1600번 : 말이 되고픈 원숭이 (1) | 2024.03.05 |