
import sys
from collections import deque
def bfs(x, y, color):
cnt = 0
queue = deque()
queue.append((x, y))
graph[x][y] = '0'
while queue:
x, y = queue.popleft()
cnt += 1
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if (0 <= nx < m and 0 <= ny < n):
# 방문한적이 없으면 삽입, 방문 처리
if(graph[nx][ny] == color):
queue.append((nx, ny))
graph[nx][ny] = '0'
return cnt
n, m = map(int, sys.stdin.readline().split()) # n = 가로, m = 세로
graph = [list(sys.stdin.readline()) for _ in range(m)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
total_W = 0
total_B = 0
for i in range(m):
for j in range(n):
if (graph[i][j] == 'W'):
total_W += (bfs(i, j, 'W')) ** 2
elif (graph[i][j] == 'B'):
total_B += (bfs(i, j, 'B')) ** 2
print(total_W, total_B)
오늘 느낀것
graph[x][y] = '0'
graph[nx][ny] = '0'
그래프를 만들때 int 를 안쓰고 그냥 쓰다 보니 그냥 0을 적었을 때는 계속 안되었는데 0을 문자열로 만드니 코드가 작성이 되었다.
그리고 오늘은 새롭게 배운것 이 있다.
바로 cs 관련 해서 새롭게 배웠다.


(낙서무엇;;)
글씨가 개판이지만 일단 나만 알아보면 되지 않은가
오늘 배운것들은 굉장히 어떻게 보면 기초적인 것들이었다.
하지만 난 이름만 들어봤지 어떤 역할을 하는지는 잘 몰랐다.
오늘 배운것들을 바탕으로 공부를 해야할 것 같다.