728x90
문제 링크
https://www.acmicpc.net/problem/14502
코드
from itertools import combinations
from collections import deque
import copy
n, m = map(int, input().split())
graph = []
for _ in range(n):
graph.append(list(map(int, input().split())))
virus = [] # 바이러스의 좌표
empty = [] # 비어있는 0 좌표
for i in range(n):
for j in range(m):
if graph[i][j] == 2:
virus.append([i,j])
elif graph[i][j] == 0:
empty.append([i,j])
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
def bfs(graph, x, y):
que = deque()
que.append((x,y))
while que:
x, y = que.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if graph[nx][ny] == 0:
graph[nx][ny] = 2
que.append((nx, ny))
result = 0
for walls in combinations(empty, 3):
cnt = 0
copy_graph = copy.deepcopy(graph)
for x, y in walls: # 벽 세우기
copy_graph[x][y] = 1
for x, y in virus: # 바이러스 퍼트리기
bfs(copy_graph, x, y)
for row in range(n):
cnt += copy_graph[row].count(0)
result = max(result, cnt)
print(result)
728x90
'코딩테스트' 카테고리의 다른 글
BFS&DFS_심화18) 괄호변환 _ python (0) | 2023.09.25 |
---|---|
BFS&DFS_심화17) 경쟁적 전염 _ python (0) | 2023.09.25 |
BFS&DFS_심화15) 특정 거리의 도시 찾기 _ python (0) | 2023.09.25 |
백트래킹(Backtracking) (1) | 2023.09.25 |
3주차 - DFS/BFS (0) | 2023.09.25 |