BOJ - 2636 - 치즈

Updated:

import sys
from collections import deque

def BFS(maps, W, H):
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]
    time, cheese = 0, 0

    while True:
        temp = deque()
        dq = deque()
        dq.append((0, 0))
        while dq:
            x, y = dq.popleft()
            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]

                if nx < 0 or nx >= H + 2 or ny < 0 or ny >= W + 2:
                    continue
                if maps[nx][ny] == 0:
                    maps[nx][ny] = -1
                    dq.append((nx, ny))
                if maps[nx][ny] == 1:
                    maps[nx][ny] = -1
                    temp.append((nx, ny))

        if not temp:
             break

        time += 1
        cheese = len(temp)
        for i in range(H + 2):
            for j in range(W + 2):
                if maps[i][j] == -1:
                    maps[i][j] = 0
    print(time)
    print(cheese)

def solution():
    maps = []
    H, W = map(int, sys.stdin.readline().split())

    maps.append([0] * (W+2))
    for _ in range(H):
        maps.append([0] + list(map(int, sys.stdin.readline().split() + [0])))
    maps.append([0] * (W + 2))

    BFS(maps, W, H)

solution()

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

Categories:

Updated:

Leave a comment