https://www.acmicpc.net/problem/10026
import copy
n = int(input())
data = [list(input()) for _ in range(n)]
red_green = copy.deepcopy(data)
for i in range(n):
for j in range(n):
if red_green[i][j] == 'G':
red_green[i][j] = 'R'
dx = [0,1,0,-1]
dy = [1,0,-1,0]
def dfs(data,x,y,color):
q = [(x,y)]
data[x][y] = 0
check = [[0]*n for _ in range(n)]
check[x][y] = 1
while q:
x,y = q.pop()
for i in range(4):
nx,ny = x+dx[i],y+dy[i]
if 0<=nx<n and 0<=ny<n and check[nx][ny]==0 and data[nx][ny] == color:
q.append((nx,ny))
check[nx][ny] = 1
data[nx][ny] = 0
count = 0
count_rg = 0
for i in range(n):
for j in range(n):
if data[i][j] == 'R':
dfs(data,i,j,'R')
count += 1
if data[i][j] == 'B':
dfs(data,i,j,'B')
count += 1
if data[i][j] == 'G':
dfs(data,i,j,'G')
count += 1
if red_green[i][j] == 'R':
dfs(red_green,i,j,'R')
count_rg += 1
if red_green[i][j] == 'B':
dfs(red_green,i,j,'B')
count_rg += 1
print(count, count_rg)
'알고리즘' 카테고리의 다른 글
백준 2468번 : 안전 영역(python) (0) | 2021.07.26 |
---|---|
백준 2583번 : 영역 구하기(python) (0) | 2021.07.26 |
백준 2667번 : 단지번호붙이기(python) (0) | 2021.07.26 |
백준 7576번 : 토마토(python) (0) | 2021.07.26 |
백준 1600번 : 말이 되고픈 원숭이(Python) (0) | 2021.06.20 |