-
Notifications
You must be signed in to change notification settings - Fork 0
/
b10026.py
103 lines (96 loc) · 3.31 KB
/
b10026.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 0:46
def solution(n, arr):
n1 = normal(n, arr)
n2 = abnormal(n, arr)
print(n1, n2)
def abnormal(n, arr):
area = 0
visit = [[0 for j in range(len(arr[0]))] for i in range(len(arr))]
queue = []
for a in range(n):
arr[a] = arr[a].replace("R", "G")
while True:
nextI = 0
nextJ = 0
checked = False
for i in range(n):
if checked: break
for j in range(n):
if visit[i][j] == 0:
nextI = i
nextJ = j
checked = True
break
if checked is False:
break
queue.append([nextI, nextJ])
visit[nextI][nextJ] = 1
while len(queue) > 0:
q = queue.pop(0)
color = arr[q[0]][q[1]]
# 인접 노드 중 색깔 같은게 있는지 검사
if q[0] > 0:
if visit[q[0] - 1][q[1]] == 0 and arr[q[0] - 1][q[1]] == color:
queue.append([q[0] - 1, q[1]])
visit[q[0] - 1][q[1]] = 1
if q[0] + 1 < n:
if visit[q[0] + 1][q[1]] == 0 and arr[q[0] + 1][q[1]] == color:
queue.append([q[0] + 1, q[1]])
visit[q[0] + 1][q[1]] = 1
if q[1] > 0:
if visit[q[0]][q[1] - 1] == 0 and arr[q[0]][q[1] - 1] == color:
queue.append([q[0], q[1] - 1])
visit[q[0]][q[1] - 1] = 1
if q[1] + 1 < n:
if visit[q[0]][q[1] + 1] == 0 and arr[q[0]][q[1] + 1] == color:
queue.append([q[0], q[1] + 1])
visit[q[0]][q[1] + 1] = 1
area += 1
return area
def normal(n, arr):
area = 0
visit = [[0 for j in range(len(arr[0]))] for i in range(len(arr))]
queue = []
while True:
nextI = 0
nextJ = 0
checked = False
for i in range(n):
if checked: break
for j in range(n):
if visit[i][j] == 0:
nextI = i
nextJ = j
checked = True
break
if checked is False:
break
queue.append([nextI, nextJ])
visit[nextI][nextJ] = 1
while len(queue) > 0:
q = queue.pop(0)
color = arr[q[0]][q[1]]
# 인접 노드 중 색깔 같은게 있는지 검사
if q[0] > 0:
if visit[q[0] - 1][q[1]] == 0 and arr[q[0] - 1][q[1]] == color:
queue.append([q[0] - 1, q[1]])
visit[q[0] - 1][q[1]] = 1
if q[0] + 1 < n:
if visit[q[0] + 1][q[1]] == 0 and arr[q[0] + 1][q[1]] == color:
queue.append([q[0] + 1, q[1]])
visit[q[0] + 1][q[1]] = 1
if q[1] > 0:
if visit[q[0]][q[1] - 1] == 0 and arr[q[0]][q[1] - 1] == color:
queue.append([q[0], q[1] - 1])
visit[q[0]][q[1] - 1] = 1
if q[1] + 1 < n:
if visit[q[0]][q[1] + 1] == 0 and arr[q[0]][q[1] + 1] == color:
queue.append([q[0], q[1] + 1])
visit[q[0]][q[1] + 1] = 1
area += 1
return area
n = int(input())
arr = []
for i in range(n):
arr.append(input())
solution(n, arr)