Advertisement
viligen

max_sum_submatrix3x3_in_matrix

Jan 21st, 2022
776
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from sys import maxsize
  2.  
  3. rows, cols = [int(n) for n in input().split()]
  4.  
  5. matrix = [[int(n) for n in input().split()] for i in range(rows)]
  6.  
  7. max_sum = -maxsize
  8. max_submatrix = []
  9.  
  10. for i in range(rows - 2):
  11.     for j in range(len(matrix[i]) - 2):
  12.         current_submatrix = [matrix[i][j], matrix[i][j+1], matrix[i][j+2],
  13.                              matrix[i+1][j], matrix[i+1][j+1], matrix[i+1][j+2],
  14.                              matrix[i + 2][j], matrix[i + 2][j + 1], matrix[i + 2][j + 2]]
  15.         current_sum = sum(current_submatrix)
  16.         if current_sum > max_sum:
  17.             max_sum = current_sum
  18.             max_submatrix = current_submatrix.copy()
  19.  
  20. print(f"Sum = {max_sum}")
  21. print(*max_submatrix[:3])
  22. print(*max_submatrix[3:6])
  23. print(*max_submatrix[6:])
  24.  
  25.  
  26.  
  27.  
  28.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement