๐๋ฌธ์
https://www.acmicpc.net/problem/4179
๐์ด๋ค ์๊ณ ๋ฆฌ์ฆ?
1. ๋ถ์ ๊ฐ ์ง์ ์์ ๋ค ๋ฐฉํฅ์ผ๋ก ํ์ฐ๋๋ค -> bfs ์์ ์ ์ ์๋ค.
2. ์งํ์ด์ ๋ถ์ ๋งค ๋ถ๋ง๋ค ํ์นธ์ฉ ์ํ ๋๋ ์์ง์ผ๋ก ์ด๋ํ๋ค. -> bfs ์์ ์ ์ ์๋ค.
3. ๋ถ์ด ํ๊ธฐ์ ์ ํ์ถํ๋ ค๋ฉด ๋ถ์ด ์ธ์ ์ด๋๊น์ง ํผ์ง๋์ง ์ ์ ์์ด์ผ ํ๋ค. ๋ถ์ด ์ธ์ ์ด๋๊น์ง ํผ์ง๋์ง graph์ ํ์ํ ๋ค, ์งํ์ด์ ์ด๋์๊ฐ์ ๋ฎ์ด์์์ฃผ๋ฉด ๋จ!
4. ๋ถ์ด ๋ฒ์ง์ง ์์ ๊ณณ์ ์ฌ์ ํ ํด๋น ์ขํ์ ๊ฐ์ด '.' ์์ ๊ผญ ๊ธฐ์ตํ๊ณ ์ฝ๋ ์์ฑํ๊ธฐ!
4. ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ๊ธ์ ๋ฐ์ ์์
๐ป์ฝ๋
from collections import deque
def f_bfs():
while f_deq:
x,y = f_deq.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
# graph ๋ฒ์ด๋ ์ขํ๋ ๋ฌด์
if nx < 0 or nx > (r-1) or ny < 0 or ny > (c-1):
continue
'''
'J'์ ํด๋นํ๋ ์ขํ๋ 0์ผ๋ก ๋ฏธ๋ฆฌ ๋ฐ๊ฟ๋๊ธฐ ๋๋ฌธ์
graph[nx][ny] == '.'์ผ ๋๋ง ๋ถ์ด ์ด๋ํ ์ ์๋๋ก ํจ.
'''
if graph[nx][ny] == '.':
graph[nx][ny] = graph[x][y] + 1
f_deq.append([nx,ny])
def j_bfs():
while j_deq:
x,y = j_deq.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
# graph๋ฅผ ๋ฒ์ด๋๋ฉด ํ์ถํ์์ ์๋ฏธํ๋ค.
if nx < 0 or nx > (r-1) or ny < 0 or ny > (c-1):
time = graph[x][y] + 1
return print(time) # ํ์ถ!
#๋ถ์ด ๋ฒ์ง์ง ์์์ผ๋ฉด ํด๋น ์ขํ๊ฐ์ด '.'์ธ ๊ฒฝ์ฐ๋ ์์์ ๊ณ ๋ ค
if graph[nx][ny] == '.':
graph[nx][ny] = graph[x][y] + 1
j_deq.append([nx,ny])
# ํด๋น ์ขํ๊ฐ์ด '#'์ด๋ฉด ๋ฐ์ if๋ฌธ์ด ์ค๋ฅ๊ฐ ๋์ ๊ฑธ๋ฌ์ฃผ๊ธฐ ์ํด ์ถ๊ฐ
elif graph[nx][ny] != '#':
if graph[nx][ny] > (graph[x][y] + 1): # ๋ถ์ ๊ฐ์ด ์งํ์ด๋ณด๋ค ํฌ๋ฉด
graph[nx][ny] = graph[x][y] + 1
j_deq.append([nx,ny])
time = 'IMPOSSIBLE' # ์งํ์ด๊ฐ ํ์ถํ์ง ๋ชปํ๋ฉด ์ด ๋ฌธ์ฅ์ ๋๋ฌ
return print(time)
# r: ํ(x), c: ์ด(y)
r,c = map(int,input().split())
# graph
graph = []
for i in range(r):
graph.append(list(input()))
#deque
j_deq = deque()
f_deq = deque()
dx = [0,0,-1,1]
dy = [-1,1,0,0]
# ์งํ, ๋ถ ์ฒ์ ์์น ํ์ ๋ฃ๊ธฐ
for i in range(r):
for j in range(c):
if graph[i][j] == 'F':
f_deq.append([i,j])
graph[i][j] = 0 # 0๋ถํฐ ์์ํด์ ํด๋น ์์น ์๊ฐ ํ์
if graph[i][j] == 'J':
j_deq.append([i,j])
graph[i][j]= 0 # 0๋ถํฐ ์์ํด์ ํด๋น ์์น ์๊ฐ ํ์
f_bfs() # ๋ถ ๋จผ์ bfs๋๋ ค์ ์ธ์ ์ด๋๊น์ง ํผ์ง๋์ง graph์ ํ๊ธฐ
j_bfs()
โ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ
์ฌ์ค ์ด ๋ฌธ์ ๋ฅผ ๋ณด๊ฒ๋๋ฉด ๊ฐ์ฅ ๋จผ์ ๋ค์๋ ์๊ฐ์ด fire๋ฆฌ์คํธ์ jihoon๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด์ ์ด๋ํ ํ์ ์ ์ฌ๊ธฐ์ ๋จ๊ธธ๊น? ์๋ค. ๊ทธ๋ฌ๋ graph์ fire๋ฆฌ์คํธ, jihoon๋ฆฌ์คํธ๊น์ง ๊ด๋ฆฌํ๊ฒ ๋์, ๋ฐ๋ก ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ์ค๋ฅ๊ฐ ๋ด๊ณ ,, ใ ใ ๊ทธ๋์ ์ฝ๋๊ฐ ์กฐ๊ธ ์ง์ ๋ถํ์ง๋ง graph๋ด์ ๋ถ๊ณผ ์งํ์ด์ ์๊ฐ์ ๋ชจ๋ ํ๊ธฐํ๋ ์ฝ๋๋ก ๋ค ๊ฐ์์์๋ค.
๐ป๋ฉ๋ชจ๋ฆฌ ์ค๋ฅ ์ฝ๋
from collections import deque
def fire_bfs():
while f_deq:
x,y = f_deq.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
# graph ๋ฒ์ด๋ ์ขํ๋ ๋ฌด์
if nx < 0 or nx > (r-1) or ny < 0 or ny > (c-1):
continue
if graph[nx][ny] == '#' or fire[nx][ny] > 0:
continue
if graph[nx][ny] == '.' or graph[nx][ny] == 'J':
fire[nx][ny] = fire[x][y] + 1
f_deq.append([nx,ny])
def jihoon_bfs():
# print('jihoon')
while j_deq:
x,y = j_deq.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx > (r-1) or ny < 0 or ny > (c-1): # ํ์ถ
time = jihoon[x][y] + 1
return print(time)
if graph[nx][ny] == '#' or fire[nx][ny] <= (jihoon[x][y]+1):
continue
if graph[nx][ny] == '.' or graph[nx][ny] == 'F':
jihoon[nx][ny] = jihoon[x][y] + 1
j_deq.append([nx,ny])
time = 'IMPOSSIBLE' # ํ์ถ ๋ชป ํ๋ฉด ์คํ๋จ
return print(time)
# r: ํ(x), c: ์ด(y)
r,c = map(int,input().split())
# graph
graph = []
for i in range(r):
graph.append(list(input()))
# ๋ถ, ์งํ ์์น ํ๊ธฐํ ๋ฆฌ์คํธ
jihoon = [[0 for j in range(c)] for i in range(r)]
fire = [[0 for j in range(c)] for i in range(r)]
#deque
j_deq = deque()
f_deq = deque()
dx = [0,0,-1,1]
dy = [-1,1,0,0]
# ์งํ, ๋ถ ์ฒ์ ์์น ํ์ ๋ฃ๊ธฐ
for i in range(r):
for j in range(c):
if graph[i][j] == 'F':
f_deq.append([i,j])
if graph[i][j] == 'J':
j_deq.append([i,j])
fire_bfs()
jihoon_bfs()
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 9498 ์ํ ์ฑ์ (ํ์ด์ฌ/python) (0) | 2023.06.19 |
---|---|
[๋ฐฑ์ค] 1330 ๋ ์ ๋น๊ตํ๊ธฐ (ํ์ด์ฌ/python) (0) | 2023.06.19 |
[๋ฐฑ์ค] 7576 ํ ๋งํ (ํ์ด์ฌ/python) (0) | 2023.06.19 |
[๋ฐฑ์ค] 2178 ๋ฏธ๋ก (ํ์ด์ฌ/python) (0) | 2023.06.19 |
[๋ฐฑ์ค] 1926 ๊ทธ๋ฆผ (ํ์ด์ฌ/python) (0) | 2023.06.17 |