Advertisement
d_brezoev

Patterns

Jan 24th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 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 Task3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {  
  13.             //sizes of pattern            
  14.             const int heightSize = 3;
  15.             const int lengthSize = 5;
  16.  
  17.             int n = int.Parse(Console.ReadLine());
  18.             int[,] matrix = new int[n, n];
  19.             int rows = n;
  20.             int cols = n;
  21.             for (int row = 0; row < rows; row++)
  22.             {
  23.                 string line = Console.ReadLine();
  24.                 string[] lineArr = line.Split(' ');
  25.                 for (int col = 0; col < cols; col++)
  26.                 {
  27.                     matrix[row, col] = int.Parse(lineArr[col]);
  28.                 }
  29.             }
  30.                                    
  31.             int maxSum = int.MinValue;
  32.             int currentSum = 0;            
  33.            
  34.             for (int row = 0; row < matrix.GetLength(0)-heightSize+1; row++)
  35.             {
  36.                 int[,] bigMatrix = new int[3, 5];
  37.                 for (int col = 0; col < matrix.GetLength(1)-lengthSize+1; col++)
  38.                 {
  39.                     for (int i = 0; i < bigMatrix.GetLength(0); i++)
  40.                     {
  41.                         for (int k = 0; k < bigMatrix.GetLength(1); k++)
  42.                         {
  43.                             currentSum += matrix[row + i, col + k];
  44.                             bigMatrix[i,k] = matrix[row + i, col + k];                            
  45.                         }
  46.                     }
  47.                     if (!IsSequened(bigMatrix))
  48.                     {
  49.                         currentSum = 0;
  50.                         continue;
  51.                     }
  52.                     currentSum -= bigMatrix[0, 3];
  53.                     currentSum -= bigMatrix[0, 4];
  54.                     currentSum -= bigMatrix[1, 3];
  55.                     currentSum -= bigMatrix[1, 4];
  56.                     currentSum -= bigMatrix[1, 0];
  57.                     currentSum -= bigMatrix[1, 1];
  58.                     currentSum -= bigMatrix[2, 0];
  59.                     currentSum -= bigMatrix[2, 1];
  60.                     if (currentSum > maxSum)
  61.                     {
  62.                         maxSum = currentSum;                        
  63.                         currentSum = 0;
  64.                     }
  65.                 }
  66.             }
  67.             Console.Write("YES ");
  68.             Console.WriteLine(maxSum);
  69.         }
  70.  
  71.         private static bool IsSequened(int[,] bigMatrix)
  72.         {
  73.             if (bigMatrix[0,0] == bigMatrix[0,1]-1 &&
  74.                 bigMatrix[0,1] == bigMatrix[0,2]-1 &&
  75.                 bigMatrix[0,2] == bigMatrix[1,2]-1 &&
  76.                 bigMatrix[1,2] == bigMatrix[2,2]-1 &&
  77.                 bigMatrix[2,2] == bigMatrix[2,3]-1 &&
  78.                 bigMatrix[2,3] == bigMatrix[2,4]-1)
  79.  
  80.             {
  81.                 return true;
  82.             }
  83.             return false;
  84.         }        
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement