fork download
  1. # your code goes here
  2. #0JLQsNGB0LjQu9C10L3QutC+INCQ0YDRgtC10Lwg
  3.  
  4. import random
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. from matplotlib.colors import ListedColormap
  8. import matplotlib.patches as mpatches
  9.  
  10. N = 40
  11. M = 40
  12. TREE_CHANCE = 0.15
  13. GARAGE_H = 4
  14. GARAGE_W = 5
  15.  
  16. EMPTY = 0
  17. TREE = 1
  18. GARAGE = 2
  19.  
  20. def solve_garages_simple_plot():
  21. grid = np.zeros((N, M), dtype=int)
  22.  
  23. for r in range(N):
  24. for c in range(M):
  25. if random.random() < TREE_CHANCE:
  26. grid[r, c] = TREE
  27.  
  28. print(f"Поле {N}x{M}. Дерев: {TREE_CHANCE*100}%.")
  29. print(f"Гараж: {GARAGE_H}x{GARAGE_W}")
  30.  
  31. garages_count = 0
  32.  
  33. for r in range(N):
  34. for c in range(M):
  35. if r + GARAGE_H > N or c + GARAGE_W > M:
  36. continue
  37.  
  38. area = grid[r:r+GARAGE_H, c:c+GARAGE_W]
  39. if np.any(area != EMPTY):
  40. continue
  41.  
  42. grid[r:r+GARAGE_H, c:c+GARAGE_W] = GARAGE
  43. garages_count += 1
  44.  
  45. print(f"Розміщено гаражів: {garages_count}")
  46.  
  47. colors = ['#f0f0f0', '#228B22', '#4169E1']
  48. cmap = ListedColormap(colors)
  49.  
  50. fig, ax = plt.subplots(figsize=(12, 8))
  51.  
  52. ax.imshow(grid, cmap=cmap, vmin=0, vmax=2)
  53.  
  54. ax.set_xticks(np.arange(-0.5, M, 1), minor=True)
  55. ax.set_yticks(np.arange(-0.5, N, 1), minor=True)
  56. ax.grid(which='minor', color='white', linestyle='-', linewidth=0.5)
  57. ax.tick_params(which="minor", bottom=False, left=False)
  58.  
  59. ax.set_title(f'План забудови: {garages_count} гаражів\n(Поле: {N}x{M}, Гараж: {GARAGE_H}x{GARAGE_W})', fontsize=14)
  60. ax.set_xlabel('M')
  61. ax.set_ylabel('N')
  62.  
  63. legend_patches = [
  64. mpatches.Patch(color=colors[0], label='Вільна ділянка'),
  65. mpatches.Patch(color=colors[1], label='Дерево'),
  66. mpatches.Patch(color=colors[2], label='Гараж')
  67. ]
  68. ax.legend(handles=legend_patches, bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0.)
  69.  
  70. plt.tight_layout()
  71. plt.show()
  72.  
  73. if __name__ == "__main__":
  74. solve_garages_simple_plot()
  75.  
Success #stdin #stdout #stderr 3.73s 72828KB
stdin
Standard input is empty
stdout
Поле 40x40. Дерев: 15.0%.
Гараж: 4x5
Розміщено гаражів: 11
stderr
Fontconfig error: No writable cache directories