๋ฌธ์
https://www.acmicpc.net/problem/4179
ํ์ด
# ๋ฐฑ์ค 4179๋ฒ ๋ฌธ์ - ๋ถ!
import sys
from collections import deque
input = sys.stdin.readline
n, m = map(int, input().split())
graph = []
for i in range(n):
graph.append(list(input().rstrip()))
if 'J' in graph[i]:
q = deque([(0, i, graph[i].index('J'))])
for i in range(n):
for j in range(m):
if graph[i][j] == 'F':
q.append((-1, i, j))
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
ans = 'IMPOSSIBLE'
while q:
time, x, y = q.popleft()
# ์งํ์ด ํ์ถ
if time > -1 and graph[x][y] != 'F' and (x == 0 or y == 0 or x == n - 1 or y == m - 1):
ans = time + 1
break
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m and graph[nx][ny] != '#':
# ์งํ์ด ์ด๋
if time > -1 and graph[nx][ny] == '.':
graph[nx][ny] = '_'
q.append((time + 1, nx, ny))
# ๋ถ ํผ๋จ๋ฆฌ๊ธฐ
elif time == -1 and graph[nx][ny] != 'F':
graph[nx][ny] = 'F'
q.append((-1, nx, ny))
print(ans)
๋ฌธ์ ๋ฅผ ํ๋ค๊ฐ ๋์ ํ ์ดํด๊ฐ ์๋์ ๊ตฌ๊ธ๋ง์ ํ๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 5052๋ฒ_์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2023.04.14 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Silver4] 1302๋ฒ_๋ฒ ์คํธ์ ๋ฌ (0) | 2023.04.12 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver5] 7785๋ฒ_ํ์ฌ์ ์๋ ์ฌ๋ (0) | 2023.04.11 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver4] 11652๋ฒ_์นด๋ (0) | 2023.04.10 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver5] 11004๋ฒ_K๋ฒ์งธ ์ (0) | 2023.04.09 |