ABOUT ME

Today
Yesterday
Total
  • [복습] 14502 연구소
    코테 대비 python/백준 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)

    '코테 대비 python > 백준' 카테고리의 다른 글

    백준 2615 오목  (0) 2023.04.21
    [복습] 15649 n과 m 1  (1) 2023.04.17
    18115 카드 놓기  (0) 2023.04.16
    2580 스도쿠  (0) 2023.04.14
    복습 1935 후위 표기식2  (0) 2023.04.14
Designed by Tistory.