티스토리 뷰
https://www.acmicpc.net/problem/10026
from collections import deque
# 상, 하, 좌, 우
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
def bfs(x, y):
q.append([x, y])
check[x][y] = 1
while(q):
x, y = q.popleft()
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < N and 0 <= ny < N:
if s[nx][ny] == s[x][y] and check[nx][ny] == 0:
q.append([nx, ny])
check[nx][ny] = 1
N = int(input())
s = [list(input()) for _ in range(N)]
check = [[0] * N for _ in range(N)]
q = deque()
cnt = 0
for i in range(N):
for j in range(N):
if check[i][j] == 0:
bfs(i, j)
cnt += 1
print(cnt, end = ' ')
for i in range(N):
for j in range(N):
if s[i][j] == 'G':
s[i][j] = 'R'
check = [[0] * N for _ in range(N)]
cnt = 0
for i in range(N):
for j in range(N):
if check[i][j] == 0:
bfs(i, j)
cnt += 1
print(cnt)
'Algorithm > Baekjoon' 카테고리의 다른 글
[백준] 10546 : 배부른 마라토너 - Python (0) | 2020.07.17 |
---|---|
[백준] 10988 : 팰린드롬인지 확인하기 - Python (0) | 2020.07.15 |
[백준] 11404 : 플로이드 - Python (0) | 2020.07.10 |
[백준] 11403 : 경로 찾기 - Python (0) | 2020.07.09 |
[백준] 14503 : 로봇 청소기 - Python (0) | 2020.07.07 |