728x90
from collections import deque
N= int(input())
picture1 = [[] for _ in range (0,N)]
picture2 = [[] for _ in range (0,N)]
for i in range (0,N):
a = list(input())
for j in range (0,N):
if a[j] == "B":
picture1[i].append(a[j])
picture2[i].append(a[j])
else:
picture1[i].append(a[j])
picture2[i].append("G") #R이 G가 된다고 생각하자.
visited1 = [[False for _ in range (0,N)] for _ in range (0,N)]
visited2 = [[False for _ in range (0,N)] for _ in range (0,N)]
dx,dy = [0,0,1,-1],[1,-1,0,0]
cnt1,cnt2 = 0,0
for i in range (0,N):
for j in range (0,N):
if visited1[i][j] == False:
cnt1 += 1
deq = deque()
deq.appendleft([i,j])
while deq:
x = deq.popleft()
visited1[x[0]][x[1]] = True
for k in range (0,4):
a = x[0] + dx[k]
b = x[1] + dy[k]
if 0 <= a < N and 0 <= b < N and visited1[a][b] == False and picture1[a][b] == picture1[x[0]][x[1]]:
deq.appendleft([a,b])
if visited2[i][j] == False:
cnt2 += 1
deq = deque()
deq.appendleft([i,j])
while deq:
x = deq.popleft()
visited2[x[0]][x[1]] = True
for k in range (0,4):
a = x[0] + dx[k]
b = x[1] + dy[k]
if 0 <= a < N and 0 <= b < N and visited2[a][b] == False and picture2[a][b] == picture2[x[0]][x[1]]:
deq.appendleft([a,b])
print(cnt1,cnt2)
그냥 조오금 복잡할 뻔한 BFS / DFS 유형이었다
'코딩' 카테고리의 다른 글
[BOJ] 28449 누가 이길까 (1) | 2024.02.07 |
---|---|
[BOJ] 1503 세 수 고르기 Python (0) | 2024.01.10 |
[BOJ] 7569 토마토 Python (0) | 2024.01.05 |
[BOJ] 2312 수 복원하기 Python (1) | 2024.01.04 |
[BOJ] 1334 다음 팰린드롬 수 (0) | 2023.12.31 |