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
|
TARGET = '1'
VISITED = -1
cost_list = []
directions = [
[0, 1],
[0, -1],
[1,0],
[-1,0]
]
def dfs(x,y):
global n
map_list[x][y] = VISITED
localCount = 1
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 map_list[nx][ny] != TARGET:
continue
localCount += dfs(nx,ny)
return localCount
for i in range(n):
for j in range(n):
if map_list[i][j] != TARGET:
continue
cost_list.append(dfs(i,j))
cost_list.sort()
print(len(cost_list))
for i in cost_list:
print(i)
|