Advertisement
yanass

Maximal Sum

Sep 30th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Square_with_maximum_sum
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int[] matrixDimensions = Console.ReadLine()
  11.                 .Split(" ")
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.  
  15.             int[,] matrix = new int[matrixDimensions[0], matrixDimensions[1]];
  16.  
  17.             int squareSize = 3; // it can be different number
  18.  
  19.  
  20.  
  21.             for (int row = 0; row < matrix.GetLength(0); row++)
  22.             {
  23.                 int[] colElements = Console.ReadLine()
  24.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  25.                     .Select(int.Parse)
  26.                     .ToArray();
  27.  
  28.                 for (int col = 0; col < matrix.GetLength(1); col++)
  29.                 {
  30.                     matrix[row, col] = colElements[col];
  31.                 }
  32.             }
  33.  
  34.             int maxSum = int.MinValue;
  35.             int upperLeftRow = -1;
  36.             int leftColumn = -1;
  37.  
  38.             for (int row = 0; row < matrix.GetLength(0) - squareSize + 1; row++)
  39.             {
  40.                 for (int col = 0; col < matrix.GetLength(1) - squareSize + 1; col++)
  41.                 {
  42.                     int currentSum = MaxSum(row, col, squareSize, matrix);
  43.  
  44.                     if (maxSum < currentSum)
  45.                     {
  46.                         maxSum = currentSum;
  47.                         upperLeftRow = row;
  48.                         leftColumn = col;
  49.                     }
  50.                 }
  51.             }
  52.  
  53.             Console.WriteLine("Sum = " + maxSum);
  54.  
  55.             for (int row = upperLeftRow; row < upperLeftRow + squareSize; row++)
  56.             {
  57.                 for (int col = leftColumn; col < leftColumn + squareSize; col++)
  58.                 {
  59.                     Console.Write(matrix[row, col] + " ");
  60.                 }
  61.                 Console.WriteLine();
  62.             }
  63.  
  64.         }
  65.  
  66.         static int MaxSum(int row, int col, int size, int[,] matrix)
  67.         {
  68.             int sum = 0;
  69.             int rows = row + size;
  70.             int cols = col + size;
  71.             for (int r = row; r < rows; r++)
  72.             {
  73.                 for (int c = col; c < cols; c++)
  74.                 {
  75.                     sum += matrix[r, c];
  76.                 }
  77.             }
  78.             return sum;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement