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
46
47
48
49
50
51
52
53
54
55
56
57
| import sys
sys.setrecursionlimit(10000)
DIRECTIONS = [[0,1], [0,-1], [1,0], [-1,0]]
VISITED = 1
def dfs(x,y,target, visit_map, n):
global VISITED
for i in range(4):
nx = x + DIRECTIONS[i][0]
ny = y + DIRECTIONS[i][1]
if nx < 0 or ny < 0 or nx >= n or ny >= n:
continue
if visit_map[nx][ny] != target:
continue
visit_map[nx][ny] = VISITED
dfs(nx,ny,target,visit_map,n)
def copy_map(source_map, n,is_red_green_blind):
visit_map = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
visit_map[i][j] = source_map[i][j]
if is_red_green_blind and visit_map[i][j] == 'G':
visit_map[i][j] = 'R'
return visit_map
def find_sections(visit_map, n):
count = 0
for i in range(n):
for j in range(n):
if visit_map[i][j] == VISITED:
continue
count += 1
target =visit_map[i][j]
visit_map[i][j] = VISITED
dfs(i,j,target,visit_map,n)
return count
if __name__ == "__main__":
n = int(input())
source_map = []
for _ in range(n):
source_map.append(list(input()))
visit_map = copy_map(source_map, n, False)
section_count = find_sections(visit_map, n)
print(section_count, end = " ")
visit_map = copy_map(source_map, n, True)
print(find_sections(visit_map,n))
|