Advertisement
Guest User

Digits

a guest
Mar 3rd, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         // Input
  11.         int n = int.Parse(Console.ReadLine());
  12.         int[,] matrix = new int[n, n];
  13.  
  14.         long maxSum = 0;
  15.  
  16.         for (int i = 0; i < n; i++)
  17.         {
  18.             string line = Console.ReadLine();
  19.             var numbersAsString = line.Split(' ');
  20.             for (int j = 0; j < n; j++)
  21.             {
  22.                 matrix[i, j] = int.Parse(numbersAsString[j]);
  23.             }
  24.         }
  25.  
  26.         // Going on each possible position in the matrix
  27.  
  28.         for (int row = 0; row <= matrix.GetLength(0) - 5; row++)
  29.         {
  30.             for (int col = 0; col <= matrix.GetLength(1) - 3; col++)
  31.             {
  32.  
  33.                 // Check for patterns from 3 to 9
  34.                 switch (matrix[row, col])
  35.                 {
  36.                     case 3: if (isPatternThree(row, col, matrix)) maxSum += 3;  break;
  37.                     case 4: if (isPatternFour(row, col, matrix))  maxSum += 4;  break;
  38.                     case 5: if (isPatternFive(row, col, matrix))  maxSum += 5;  break;
  39.                     case 6: if (isPatternSix(row, col, matrix))   maxSum += 6;  break;
  40.                     case 7: if (isPatternSeven(row, col, matrix)) maxSum += 7;  break;
  41.                     case 8: if (isPatternEight(row, col, matrix)) maxSum += 8;  break;
  42.                     case 9: if (isPatternNine(row, col, matrix))  maxSum += 9;  break;
  43.                 }
  44.  
  45.                 // Patern one
  46.                 if (matrix[row + 2, col] == 1)
  47.                 {
  48.                     if (isPatternOne(row, col, matrix)) maxSum += 1;
  49.                 }
  50.                 // Pattern two
  51.                 if (matrix[row + 1, col] == 2)
  52.                 {
  53.                     if (isPatternTwo(row, col, matrix)) maxSum += 2;
  54.                 }
  55.             }
  56.         }
  57.         Console.WriteLine(maxSum);
  58.     }
  59.  
  60.     private static bool isPatternOne(int row, int col, int[,] matrix)
  61.     {
  62.         return matrix[row + 1, col + 1] == 1 && matrix[row, col + 2] == 1 && matrix[row + 1, col + 2] == 1
  63.             && matrix[row + 2, col + 2] == 1 && matrix[row + 3, col + 2] == 1 && matrix[row + 4, col + 2] == 1;
  64.     }
  65.  
  66.     private static bool isPatternTwo(int row, int col, int[,] matrix)
  67.     {
  68.         return matrix[row, col + 2] == 2 && matrix[row + 1, col + 2] == 2 && matrix[row + 2, col + 2] == 2
  69.             && matrix[row + 3, col + 1] == 2 && matrix[row + 4, col] == 2 && matrix[row + 4, col + 1] == 2 && matrix[row + 4, col + 2] == 2;
  70.     }
  71.  
  72.     private static bool isPatternThree(int row, int col, int[,] matrix)
  73.     {
  74.         return matrix[row, col + 1] == 3 && matrix[row, col + 2] == 3 && matrix[row + 1, col + 2] == 3
  75.             && matrix[row + 2, col + 1] == 3 && matrix[row + 2, col + 2] == 3
  76.              && matrix[row + 2, col + 1] == 3 && matrix[row + 3, col + 2] == 3
  77.              && matrix[row + 4, col] == 3 && matrix[row + 4, col + 1] == 3 && matrix[row + 4, col + 2] == 3;
  78.     }
  79.     private static bool isPatternFour(int row, int col, int[,] matrix)
  80.     {
  81.         return matrix[row, col + 2] == 4 && matrix[row + 1, col] == 4 && matrix[row + 1, col + 2] == 4
  82.             && matrix[row + 2, col] == 4 && matrix[row + 2, col + 1] == 4
  83.              && matrix[row + 2, col + 2] == 4 && matrix[row + 4, col + 2] == 4
  84.              && matrix[row + 3, col + 2] == 4 && matrix[row + 4, col + 2] == 4;
  85.     }
  86.     private static bool isPatternFive(int row, int col, int[,] matrix)
  87.     {
  88.         return matrix[row, col + 1] == 5 && matrix[row, col + 2] == 5 && matrix[row + 1, col] == 5
  89.             && matrix[row + 2, col] == 5 && matrix[row + 2, col + 1] == 5
  90.              && matrix[row + 2, col + 2] == 5 && matrix[row + 3, col + 2] == 5
  91.              && matrix[row + 4, col] == 5 && matrix[row + 4, col + 1] == 5 && matrix[row + 4, col + 2] == 5;
  92.     }
  93.  
  94.     private static bool isPatternSix(int row, int col, int[,] matrix)
  95.     {
  96.         return matrix[row, col + 1] == 6 && matrix[row, col + 2] == 6 && matrix[row + 1, col] == 6
  97.             && matrix[row + 2, col] == 6 && matrix[row + 2, col + 1] == 6
  98.              && matrix[row + 2, col + 2] == 6 && matrix[row + 3, col] == 6 && matrix[row + 3, col + 2] == 6
  99.              && matrix[row + 4, col] == 6 && matrix[row + 4, col + 1] == 6 && matrix[row + 4, col + 2] == 6;
  100.     }
  101.  
  102.     private static bool isPatternSeven(int row, int col, int[,] matrix)
  103.     {
  104.         return matrix[row, col + 1] == 7 && matrix[row, col + 2] == 7 && matrix[row + 1, col + 2] == 7
  105.            && matrix[row + 2, col + 1] == 7 && matrix[row + 3, col + 1] == 7
  106.             && matrix[row + 4, col + 1] == 7;
  107.     }
  108.  
  109.     private static bool isPatternEight(int row, int col, int[,] matrix)
  110.     {
  111.         return matrix[row, col + 1] == 8 && matrix[row, col + 2] == 8 && matrix[row + 1, col] == 8
  112.             && matrix[row + 1, col + 2] == 8 && matrix[row + 2, col + 1] == 8
  113.              && matrix[row + 3, col] == 8 && matrix[row + 3, col + 2] == 8
  114.              && matrix[row + 4, col] == 8 && matrix[row + 4, col + 1] == 8 && matrix[row + 4, col + 2] == 8;
  115.     }
  116.  
  117.     private static bool isPatternNine(int row, int col, int[,] matrix)
  118.     {
  119.         return matrix[row, col + 1] == 9 && matrix[row, col + 2] == 9 && matrix[row + 1, col] == 9
  120.             && matrix[row + 1, col + 2] == 9 && matrix[row + 2, col + 1] == 9
  121.              && matrix[row + 2, col + 2] == 9 && matrix[row + 3, col + 2] == 9
  122.              && matrix[row + 4, col] == 9 && matrix[row + 4, col + 1] == 9 && matrix[row + 4, col + 2] == 9;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement