Advertisement
GeorgiLukanov87

08. Bombs - Multidimensional Lists - Exercise 1

Aug 27th, 2022 (edited)
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. #   08. Bombs
  2. #
  3. #   Multidimensional Lists - Exercise 1
  4. #   https://judge.softuni.org/Contests/Practice/Index/1835#7
  5.  
  6. ===================================================================
  7. Input:1
  8.    
  9. 4
  10. 8 3 2 5
  11. 6 4 7 9
  12. 9 9 3 6
  13. 6 8 1 2
  14. 1,2 2,1 2,0
  15.  
  16. Output:1
  17.    
  18. Alive cells: 3
  19. Sum: 12
  20. 8 -4 -5 -2
  21. -3 -3 0 2
  22. 0 0 -4 -1
  23. -3 -1 -1 2
  24. ===================================================================
  25. Input:2
  26.    
  27. 3
  28. 7 8 4
  29. 3 1 5
  30. 6 4 9
  31. 0,2 1,0 2,2
  32.  
  33. Output:2
  34.  
  35. Alive cells: 3
  36. Sum: 8
  37. 4 1 0
  38. 0 -3 -8
  39. 3 -8 0
  40. ===================================================================
  41. # 08. Bombs
  42.  
  43. def is_inside(current_row, current_col):  # It's square matrix =>  rows = cols
  44.     return 0 <= current_row < SIZE and 0 <= current_col < SIZE
  45.  
  46.  
  47. surroundings = [(0, 1), (0, -1), (-1, 0), (1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1)]
  48. SIZE = int(input())
  49. matrix = []
  50. for _ in range(SIZE):
  51.     matrix.append([int(x) for x in input().split()])
  52.  
  53. bombs = input().split()
  54. if matrix:
  55.     for current_bomb in bombs:
  56.         b_row, b_col = eval(current_bomb)
  57.         damage = matrix[b_row][b_col]
  58.         if damage <= 0:
  59.             continue
  60.         matrix[b_row][b_col] = 0
  61.         for direction in surroundings:
  62.             row, col = b_row + direction[0], b_col + direction[1]
  63.             if is_inside(row, col) and matrix[row][col] > 0:
  64.                 matrix[row][col] -= damage
  65.  
  66. total_sum = 0
  67. alive_cells = 0
  68.  
  69. for sub_matrix in matrix:
  70.     for num in sub_matrix:
  71.         if num > 0:
  72.             total_sum += num
  73.             alive_cells += 1
  74.  
  75. print(f'Alive cells: {alive_cells}')
  76. print(f'Sum: {total_sum}')
  77.  
  78. for sub_matrix in matrix:
  79.     print(*sub_matrix)
  80.  
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement