Advertisement
patcko

Untitled

Dec 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. classMaxPlatform2x2
  2.  
  3. {
  4.  
  5.       static void Main()
  6.  
  7.       {
  8.  
  9.             //Declare and initialize the matrix
  10.  
  11.             int[,] matrix = {
  12.  
  13.         { 0, 2, 4, 0, 9, 5 },
  14.  
  15.         { 7, 1, 3, 3, 2, 1 },
  16.  
  17.         { 1, 3, 9, 8, 5, 6 },
  18.  
  19.         { 4, 6, 7, 9, 1, 0 }
  20.  
  21.             };
  22.  
  23.  
  24.  
  25.             // Find the maximal sum platform of size 2 x 2
  26.  
  27.             int bestSum = int.MinValue;
  28.  
  29.             int bestRow = 0;
  30.  
  31.             int bestCol = 0;
  32.  
  33.  
  34.  
  35.             for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  36.  
  37.             {
  38.  
  39.                   for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  40.  
  41.                   {
  42.  
  43.                         int sum = matrix[row, col] + matrix[row, col + 1] +
  44.  
  45.                               matrix[row + 1, col] + matrix[row + 1, col + 1];
  46.  
  47.                         if (sum > bestSum)
  48.  
  49.                         {
  50.  
  51.                               bestSum = sum;
  52.  
  53.                               bestRow = row;
  54.  
  55.                               bestCol = col;
  56.  
  57.                         }
  58.  
  59.                   }
  60.  
  61.             }
  62.  
  63.  
  64.  
  65.             // Print the result
  66.  
  67.             Console.WriteLine("The best platform is:");
  68.  
  69.             Console.WriteLine("  {0} {1}",
  70.  
  71.                              matrix[bestRow, bestCol],
  72.  
  73.                              matrix[bestRow, bestCol + 1]);
  74.  
  75.                    Console.WriteLine("  {0} {1}",
  76.  
  77.                              matrix[bestRow + 1, bestCol],
  78.  
  79.                   matrix[bestRow + 1, bestCol + 1]);
  80.  
  81.             Console.WriteLine("The maximal sum is: {0}", bestSum);
  82.  
  83.       }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement