Sichanov

minesweeper venko

Jan 30th, 2022 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. def is_inside(row, col, size):
  2.     return 0 <= row < size and 0 <= col < size
  3.  
  4.  
  5. def nearly_bombs(row, col, matrix):
  6.     size = len(matrix)
  7.     count = 0
  8.     if is_inside(row - 1, col, size) and matrix[row - 1][col] == '*':
  9.         count += 1
  10.     if is_inside(row + 1, col, size) and matrix[row + 1][col] == '*':
  11.         count += 1
  12.     if is_inside(row, col - 1, size) and matrix[row][col - 1] == '*':
  13.         count += 1
  14.     if is_inside(row, col + 1, size) and matrix[row][col + 1] == '*':
  15.         count += 1
  16.     if is_inside(row - 1, col - 1, size) and matrix[row - 1][col - 1] == '*':
  17.         count += 1
  18.     if is_inside(row + 1, col + 1, size) and matrix[row + 1][col + 1] == '*':
  19.         count += 1
  20.     if is_inside(row - 1, col + 1, size) and matrix[row - 1][col + 1] == '*':
  21.         count += 1
  22.     if is_inside(row + 1, col - 1, size) and matrix[row + 1][col - 1] == '*':
  23.         count += 1
  24.     return count
  25.  
  26.  
  27. size = int(input())
  28. bombs = int(input())
  29. matrix = []
  30. for _ in range(size):
  31.     matrix.append([0] * size)
  32.  
  33. for _ in range(bombs):
  34.     bomb_row, bom_col = [int(i) for i in input().strip('()').split(', ')]
  35.     if is_inside(bomb_row, bom_col, size):
  36.         matrix[bomb_row][bom_col] = '*'
  37.  
  38. for curr_row in range(size):
  39.     for curr_col in range(size):
  40.         if matrix[curr_row][curr_col] == '*':
  41.             continue
  42.         matrix[curr_row][curr_col] = nearly_bombs(curr_row, curr_col, matrix)
  43.  
  44. for row in matrix:
  45.     print(*row, sep=' ')
Add Comment
Please, Sign In to add comment