-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj-10026.py
More file actions
45 lines (40 loc) · 1.08 KB
/
boj-10026.py
File metadata and controls
45 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
def bfs(x, y):
visited[x][y] = 1
q = deque()
q.append((x, y))
while q:
x, y = q.popleft()
dxdy = [(0, -1), (0, 1), (-1, 0), (1, 0)]
for dx, dy in dxdy:
nx = x + dx
ny = y + dy
if -1 < nx < n and -1 < ny < n:
if grid[nx][ny] == grid[x][y] and not visited[nx][ny]:
visited[nx][ny] = 1
q.append((nx, ny))
n = int(input())
grid = [list(map(str, input().strip())) for _ in range(n)]
visited = [[0 for _ in range(n)] for _ in range(n)]
ncw = 0 #색약 x
for i in range(n):
for j in range(n):
if not visited[i][j]:
bfs(i, j)
ncw += 1
print(ncw, end=' ')
cw = 0 #색약
visited = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(n):
if grid[i][j] == 'R':
grid[i][j] = 'G'
for i in range(n):
for j in range(n):
if not visited[i][j]:
bfs(i, j)
cw += 1
print(cw)