https://www.acmicpc.net/problem/14502
from collections import deque
import copy
input = sys.stdin.readline
if __name__ == "__main__":
n,m = list(map(int,input().split()))
area = [list(map(int,input().split())) for _ in range(n)]
def virus():
dx = [1,-1,0,0]
dy = [0,0,1,-1]
tmp_area = copy.deepcopy(area)
q = deque()
for i in range(n):
for j in range(m):
if tmp_area[i][j] == 2:
q.append([i,j])
while q:
x,y = q.popleft()
for k in range(4):
xx = x + dx[k]
yy = y + dy[k]
if 0<=xx<n and 0<=yy<m and tmp_area[xx][yy]==0:
q.append([xx,yy])
tmp_area[xx][yy] = 2
global result
cnt = 0
for i in range(n):
cnt += tmp_area[i].count(0)
result = max(cnt,result)
def bfs(l):
if l == 3:
virus()
return
for i in range(n):
for j in range(m):
if area[i][j]==0:
area[i][j] = 1
bfs(l+1)
area[i][j] = 0
result = 0
bfs(0)
print(result)
def virus():
dx = [1,-1,0,0]
dy = [0,0,1,-1]
tmp_area = copy.deepcopy(area)
visited = [[0]*m for _ in range(n)]
q = deque()
for i in range(n):
for j in range(m):
if tmp_area[i][j] == 2:
q.append([i,j])
visited[i][j]=1
while q:
x,y = q.popleft()
for k in range(4):
xx = x + dx[k]
yy = y + dy[k]
if 0<=xx<n and 0<=yy<m and visited[xx][yy]==0 and tmp_area[xx][yy]==0:
q.append([xx,yy])
visited[xx][yy]=1
tmp_area[xx][yy] = 2
global result
cnt = 0
for i in range(n):
cnt += tmp_area[i].count(0)
result = max(cnt,result)
2. 초반에 2를 q에 넣고 탐색하는 방법
def virus():
dx = [1,-1,0,0]
dy = [0,0,1,-1]
tmp_area = copy.deepcopy(area)
q = deque()
for i in range(n):
for j in range(m):
if tmp_area[i][j] == 2:
q.append([i,j])
while q:
x,y = q.popleft()
for k in range(4):
xx = x + dx[k]
yy = y + dy[k]
if 0<=xx<n and 0<=yy<m and tmp_area[xx][yy]==0:
q.append([xx,yy])
tmp_area[xx][yy] = 2
global result
cnt = 0
for i in range(n):
cnt += tmp_area[i].count(0)
result = max(cnt,result)
2의 방법으로 탐색하는게 시간과 메모리를 적게 잡아먹는다