코테 대비 python/백준

[복습] 14502 연구소

ylab 2023. 4. 17. 22:30
from collections import deque

n,m = map(int,input().split())
graph=[list(map(int,input().split())) for _ in range(n)]
#빈칸 위치 체크
blank=[]
virus=[]
for i in range(n):
    for j in range(m):
        if graph[i][j]==0:
            blank.append((i,j))
        elif graph[i][j]==2:
            virus.append((i,j))

#상하좌우
dx=[-1,1,0,0]
dy=[0,0,-1,1]
result=0

def bfs():
    global result
    cnt = len(blank)-3
    q=deque(virus)
    while q:
        x,y=q.popleft()
        for i in range(4):
            nx=x+dx[i]
            ny=y+dy[i]
            if 0<=nx<n and 0<=ny<m and new_graph[nx][ny]==0:
                new_graph[nx][ny]=2
                cnt-=1
                #큐에 넣어주고!!!!
                q.append((nx,ny))
    result=max(result,cnt)





#꼭 3개 만들어
from itertools import combinations
from copy import deepcopy
possi = combinations(blank,3)
for i in possi:
    new_graph=deepcopy(graph)

    for x,y in i:

        new_graph[x][y]=1

    bfs()
print(result)