Guest User

Untitled

a guest
Jan 25th, 2021
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def check_next_el(matrix, row, col):
  2. positions = [(-1, 0), (-1, -1), (-1, 1), (0, -1), (0, 1), (1, 0), (1, -1), (1, 1)]
  3.  
  4. counter = 0
  5. for position in positions:
  6.  
  7. potential_row = row + position[0]
  8. potential_col = col + position[1]
  9.  
  10. if is_valid_bound(potential_row, potential_col, len(matrix)):
  11. potential_position = matrix[potential_row][potential_col]
  12. if potential_position == '*':
  13. counter += 1
  14. return counter
  15.  
  16.  
  17. def is_valid_bound(row, col, length):
  18. return 0 <= row < length and 0 <= col < length
  19.  
  20.  
  21. size_matrix = int(input())
  22. number_bombs = int(input())
  23.  
  24.  
  25. matrix = []
  26. for i in range(size_matrix):
  27. matrix.append([])
  28. for j in range(size_matrix):
  29. matrix[i].append(0)
  30.  
  31. for _ in range(number_bombs):
  32. tokens = input()
  33. row = int(tokens[1])
  34. col = int(tokens[4])
  35. matrix[row][col] = '*'
  36.  
  37. for row_idx in range(size_matrix):
  38. for col_idx in range(size_matrix):
  39. if matrix[row_idx][col_idx] != '*':
  40. counter = check_next_el(matrix, row_idx, col_idx)
  41. matrix[row_idx][col_idx] = counter
  42.  
  43. for row in matrix:
  44. print(' '.join([str(x) for x in row]))
  45.  
RAW Paste Data