전쟁 – 전투 실버 1

2023. 1. 2. 17:08코딩 테스트 준비

728x90
반응형
from collections import deque

def bfs(y, x):
    q = deque()
    q.append((y, x))
    c = graph[y][x]
    graph[y][x] = 1
    cnt = 0
    while q:
        y, x = q.popleft()
        cnt += 1
        for dy, dx in d:
            Y, X = y+dy, x+dx
            if (0 <= Y < M) and (0 <= X < N) and graph[Y][X] == c:
                q.append((Y, X))
                graph[Y][X] = 1
    return cnt

N, M = map(int, input().split())
graph = [list(input()) for _ in range(M)]
d = [(-1, 0), (1, 0), (0, -1), (0, 1)]
w = b = 0
for i in range(M):
    for j in range(N):
        if graph[i][j] == 'W':
            w += bfs(i, j)**2
        elif graph[i][j] == 'B':
            b += bfs(i, j)**2
print(w, b)
728x90
반응형

'코딩 테스트 준비' 카테고리의 다른 글

연결 요소의 개수 – 실버 2  (0) 2023.01.02
미로 탐색 – 실버 1  (0) 2023.01.02
dfs와 bfs – 실버2  (2) 2023.01.02
The Game of Death – 실버 4  (0) 2023.01.02
Bucket Brigade – 실버 4  (0) 2023.01.02