Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import numpy as np
  2.  
  3. matrix = np.random.randint(2, size=(10, 10))
  4. print(matrix)
  5. M, N = matrix.shape
  6.  
  7. walked = []
  8. zonesCount = []
  9.  
  10.  
  11. def pathCount(x, y):
  12. if x < 0 or y < 0 or x >= M or y >= N:
  13. return 0
  14. if matrix[x, y] == 1: # I replaced * by 1 and . by 0 for easier generation
  15. return 0
  16. if (x, y) in walked:
  17. return 0
  18.  
  19. walked.append((x, y))
  20.  
  21. count = 1
  22.  
  23. for i in [x - 1, x, x + 1]:
  24. for j in [y - 1, y, y + 1]:
  25. if (i, j) != (x, y):
  26. count += pathCount(i, j)
  27.  
  28. return count
  29.  
  30. for x in range(M):
  31. for y in range(N):
  32. if not (x, y) in walked:
  33. zonesCount.append(pathCount(x, y))
  34.  
  35. print('Max zone count :', max(zonesCount))
  36.  
  37. [[0 0 1 0 0 0 1 0 1 0]
  38. [1 0 1 0 0 0 1 0 1 1]
  39. [0 1 0 0 1 0 0 1 1 1]
  40. [0 0 1 0 0 0 1 1 0 1]
  41. [1 0 1 1 1 1 0 1 1 0]
  42. [1 0 1 1 1 1 0 1 1 0]
  43. [0 0 0 1 1 1 0 0 0 0]
  44. [1 0 0 1 1 0 0 1 1 0]
  45. [0 1 0 1 0 0 1 0 1 1]
  46. [0 1 1 0 0 0 1 0 1 0]]
  47. Max zone count : 50
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement