Advertisement
tutzy

bestSumBestRowBestColl

May 17th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MaxPlatform2x2
  8. {
  9.     class bestSumBestRowBestColl
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] rowsColls = Console.ReadLine().Split(' ');
  14.             int[] rowsCollsInt = rowsColls.Select(int.Parse).ToArray();
  15.             int[,] matrix = new int[rowsCollsInt[0], rowsCollsInt[1]];
  16.             for (int i = 0; i < rowsCollsInt[0]; i++)
  17.             {
  18.                 string[] input = Console.ReadLine().Split(' ');
  19.                 for (int j = 0; j < rowsCollsInt[1]; j++)
  20.                 {
  21.                     matrix[i, j] = int.Parse(input[j]);
  22.                 }
  23.             }
  24.  
  25.             int bestSum = int.MinValue;
  26.             int bestRow = 0;
  27.             int bestColl = 0;
  28.             for (int i = 0; i < matrix.GetLength(0) - 2; i++)
  29.             {
  30.                 for (int j = 0; j < matrix.GetLength(1) - 2; j++)
  31.                 {
  32.                     int sum = matrix[i, j] + matrix[i, j + 1] + matrix[i, j + 2] + matrix[i + 1, j] + matrix[i + 1, j + 1] + matrix[i + 1, j + 2] + matrix[i + 2, j] + matrix[i + 2, j + 1] + matrix[i + 2, j + 2];
  33.                     if (sum > bestSum)
  34.                         bestSum = sum;
  35.                     {
  36.                         bestRow = i;
  37.                         bestColl = j;
  38.                     }
  39.                 }
  40.             }
  41.             Console.WriteLine("best matc:");
  42.             Console.WriteLine("{0} {1} {2}", matrix[bestRow, bestColl - 1], matrix[bestRow, bestColl], matrix[bestRow, bestColl + 1]);
  43.             Console.WriteLine("{0} {1} {2}", matrix[bestRow + 1, bestColl - 1], matrix[bestRow + 1, bestColl], matrix[bestRow + 1, bestColl + 1]);
  44.             Console.WriteLine("{0} {1} {2}", matrix[bestRow + 2, bestColl - 1], matrix[bestRow + 2, bestColl], matrix[bestRow + 2, bestColl + 1]);
  45.             Console.WriteLine("Sum = {0}", bestSum);
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement