문제
코드
from collections import deque
import sys
import copy
input = sys.stdin.readline
dx = [-1, 0, 0, 1]
dy = [0, -1, 1, 0]
def bfs():
global safe
queue = deque()
map_copy = copy.deepcopy(map)
for x in range(n):
for y in range(m):
if map_copy[x][y] == 2:
queue.append([x, y])
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m and map_copy[nx][ny] == 0:
queue.append([nx, ny])
map_copy[nx][ny] = 2
max_area_num = 0
for x in range(n):
for y in range(m):
if map_copy[x][y] == 0:
max_area_num += 1
safe = max(safe, max_area_num)
def build_wall(wall_num: int):
if wall_num != 3:
for x in range(n):
for y in range(m):
if map[x][y] == 0:
map[x][y] = 1
build_wall(wall_num + 1)
map[x][y] = 0
else:
bfs()
if __name__ == '__main__':
n, m = map(int, input().split())
map = [list(map(int, input().split())) for _ in range(n)]
safe = 0
wall = 0
build_wall(wall_num=0)
print(safe)
결과
출처 & 깃허브
boj
github