Advertisement
kalinx

bombsss

Jun 8th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. def is_index_valid(r, c):
  2.     return 0 <= r < n and 0 <= c < n
  3.  
  4.  
  5. def check_neighbors(matrix, row, col, directions):
  6.     for direction in directions:
  7.         potential_row, potential_col = row + direction[0], col + direction[1]
  8.         if is_index_valid(potential_row, potential_col):
  9.             if matrix[potential_row][potential_col] != '*':
  10.                 matrix[potential_row][potential_col] += 1
  11.  
  12.  
  13. n = int(input())
  14. n_bombs = int(input())
  15. bombs_positions = []
  16. matrix = []
  17.  
  18. for row in range(n):
  19.     matrix.append([])
  20.     for col in range(n):
  21.         matrix[row].append(0)
  22.  
  23. for bomb in range(n_bombs):
  24.     row, col = eval(input())
  25.     if is_index_valid(row, col):
  26.         bombs_positions.append([row, col])
  27.         matrix[row][col] = '*'
  28.  
  29. directions = [
  30.     [0, -1],  # left
  31.     [-1, -1],  # left up diagonal
  32.     [-1, 0],  # up
  33.     [-1, 1],  # up right diagonal
  34.     [0, 1],  # right
  35.     [1, 1],  # down right diagonal
  36.     [1, 0],  # down
  37.     [1, -1],  # down left diagonal
  38. ]
  39.  
  40. for bomb in bombs_positions:
  41.     check_neighbors(matrix, bomb[0], bomb[1], directions)
  42.  
  43.  
  44. for row in matrix:
  45.     print(*row, sep=' ')
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement