fork download
  1. # your code goes here
  2. from collections import deque
  3.  
  4.  
  5. def bfs(x, y):
  6. global a
  7. a = 0
  8. need_visited = deque()
  9. need_visited.append((x, y))
  10. dx = (1, -1, 0, 0)
  11. dy = (0, 0, 1, -1)
  12. while need_visited:
  13. x, y = need_visited.popleft()
  14. for k in range(4):
  15. nx = x + dx[k]
  16. ny = y + dy[k]
  17. if (0 <= nx < N and 0 <= ny < N) and graph[nx][ny] == 1:
  18. a += 1
  19. graph[nx][ny] = 0
  20. need_visited.append((nx, ny))
  21. A.append(a)
  22.  
  23.  
  24. N = int(input())
  25. A = []
  26. ans = 0
  27. graph = [list(map(int, input())) for _ in range(N)]
  28. for i in range(N):
  29. for j in range(N):
  30. if graph[i][j] == 1:
  31. ans += 1
  32. bfs(i, j)
  33. print(ans)
  34. A.sort()
  35. for i in A:
  36. print(i)
  37.  
Success #stdin #stdout 0.03s 9820KB
stdin
5
00000
00000
00100
00000
00000
stdout
1
0