BOJ - 4179 - 불!

Updated:

import sys
from collections import deque

def BFS(fire, person, R, C, maps, visit):
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]

    while person:
        temp = deque()
        while person:
            x, y, time = person.popleft()
            if maps[x][y] == "F":
                continue

            if x == 0 or x == R-1 or y == 0 or y == C-1:
                if maps[x][y] != "F": return maps[x][y]

            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]
                if 0 <= nx < R and 0 <= ny < C:
                    if maps[nx][ny] == "." and maps[nx][ny] != "F":
                        maps[nx][ny] = time + 1
                        temp.append((nx, ny, time + 1))
        person = temp

        temp = deque()
        while fire:
            x, y = fire.popleft()

            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]
                if 0 <= nx < R and 0 <= ny < C:
                    if visit[nx][ny] == False and maps[nx][ny] != "#":
                        maps[nx][ny] = "F"
                        visit[nx][ny] = True
                        temp.append((nx, ny))
        fire = temp
    return "IMPOSSIBLE"

def solution():
    maps = []
    fire = deque()
    person = deque()

    R, C = map(int, sys.stdin.readline().split())
    visit = [[False] * C for _ in range(R)]

    for _ in range(R):
        maps.append(list(str(sys.stdin.readline().strip())))

    for i in range(R):
        for j in range(C):
            if maps[i][j] == "J":
                person.append((i, j, 1))
                maps[i][j] = 1
            elif maps[i][j] == "F":
                fire.append((i, j))
                visit[i][j] = True
    print(BFS(fire, person, R, C, maps, visit))

solution()

https://www.acmicpc.net/problem/4179

Categories:

Updated:

Leave a comment