Advertisement
abstract_abstract

Untitled

Oct 16th, 2023
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. field = np.zeros((1024, 1024), dtype=int)
  5. x, y = 512, 512
  6.  
  7. directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
  8.  
  9. direction_index = 0
  10.  
  11. while 0 <= x < 1024 and 0 <= y < 1024:
  12.     field[y, x] = 1 - field[y, x]
  13.  
  14.     if field[y, x] == 0:
  15.         direction_index = (direction_index + 1) % 4
  16.     else:
  17.         direction_index = (direction_index - 1) % 4
  18.  
  19.     dx, dy = directions[direction_index]
  20.     x += dx
  21.     y += dy
  22.  
  23. black_cells = np.sum(field)
  24. print("Число черных клеток на пути муравья:", black_cells)
  25.  
  26. plt.imshow(field, cmap='binary')
  27. plt.axis('off')
  28. plt.savefig('ant_path.png', format='png', dpi=300)
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement