Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. rows, cols = [int(x) for x in input(" ").split()]
  2. matrix = []
  3. [matrix.append([int(x) for x in input().split(" ")]) for row in range(rows)]
  4.  
  5. max_sum = None
  6. best_matrix = None
  7.  
  8. for row in range(rows - 2):
  9. for col in range(cols - 2):
  10. first_row = matrix[row][col:col + 3]
  11. second_row = matrix[row + 1][col:col + 3]
  12. third_row = matrix[row + 2][col:col + 3]
  13.  
  14. current_sum = sum(first_row) + sum(second_row) + sum(third_row)
  15. current_matrix = [first_row] + [second_row] + [third_row]
  16.  
  17. if not max_sum:
  18. max_sum = current_sum
  19. best_matrix = current_matrix
  20. else:
  21. if current_sum > max_sum:
  22. max_sum = current_sum
  23. best_matrix = current_matrix
  24.  
  25. print(f"Sum = {max_sum}")
  26. for i in range(len(best_matrix)):
  27. current_row = [str(x) for x in best_matrix[i]]
  28. print(" ".join(current_row))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement