-
[최고빈출 DFS, BFS 기본문제] 전쟁-전투🧠𝗔𝗹𝗴𝗼𝗿𝗶𝘁𝗵𝗺/💛 백준 2021. 7. 2. 21:12
나의 풀이
from collections import deque def bfs(x, y, color): cnt = 0 queue = deque() queue.append((x, y)) visited[x][y] = True while queue: x, y = queue.popleft() 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 and not visited[nx][ny]: # each color check visited[nx][ny] = True queue.append((nx, ny)) cnt += 1 # each color count return cnt + 1 dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] n, m = map(int, input().split()) graph = [list(input()) for _ in range(m)] visited = [[False] * n for i in range(m)] white, blue = 0, 0 for i in range(m): for j in range(n): if graph[i][j] == 'W' and not visited[i][j]: white += bfs(i, j, 'W') ** 2 # count accumulate elif graph[i][j] == 'B' and not visited[i][j] : blue += bfs(i, j, 'B') ** 2 # count accumulate print(white, blue)
'🧠𝗔𝗹𝗴𝗼𝗿𝗶𝘁𝗵𝗺 > 💛 백준' 카테고리의 다른 글
[백준/2.분할정복,이분탐색] 2747번. 피보나치 수(JavaScript) (0) 2021.07.20 백준에서 JavaScript 입출력 (0) 2021.07.19 [최고빈출 DFS, BFS 기본문제] DFS와 BFS(1260번) (0) 2021.07.02 [준비운동 PART1. 튼튼한 기본기] 쉽게 푸는 문제 (1292번) (0) 2021.07.02 [준비운동 PART1. 튼튼한 기본기] 소수 찾기(1978번) (0) 2021.07.02