그냥 bfs 2번. 100*100이라 시간은 넉넉했다.
import sys
input = sys.stdin.readline
from collections import deque
def bfs(x,y,color):
visited[x][y] = True
q = deque()
q.append([x,y])
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<N and picture[nx][ny]==color and not visited[nx][ny]:
visited[nx][ny]= True
q.append([nx,ny])
def bfs2(x,y):
visited[x][y] = True
q = deque()
q.append([x,y])
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<N and (picture[nx][ny]=='R' or picture[nx][ny]=='G') and not visited[nx][ny]:
visited[nx][ny]= True
q.append([nx,ny])
N = int(input())
picture = [list(input().rstrip()) for _ in range(N)]
dx = [0,0,-1,1]
dy = [1,-1,0,0]
color = {'R': 0, 'G': 0, 'B': 0}
visited = [[False for _ in range(N)] for _ in range(N)]
for i in range(N):
for j in range(N):
if not visited[i][j]:
bfs(i,j,picture[i][j])
color[picture[i][j]] += 1
ans = 0
visited = [[False for _ in range(N)] for _ in range(N)]
for i in range(N):
for j in range(N):
if not visited[i][j] and (picture[i][j]=='R' or picture[i][j]=='G'):
bfs2(i,j)
ans += 1
print(color['R']+color['G']+color['B'],ans+color['B'])
'practivceAlgorithm > 백준' 카테고리의 다른 글
[백준][Python] 15991 1,2,3더하기 6 (0) | 2021.09.07 |
---|---|
[백준][Python] 14405 피카츄 (0) | 2021.09.07 |
[백준][Python] 9461 파도반 수열 (0) | 2021.09.07 |
[백준][Python] 9375 패션왕 신해빈 (0) | 2021.09.07 |
[백준][Python] 9019 DSLR (0) | 2021.09.07 |