Advertisement
Guest User

Squares in matrix

a guest
May 25th, 2019
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Squares_in_Matrix
  5. {
  6.     class SquaresInMatrix
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] size = Console.ReadLine().Split(' ',StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  11.             char[,] matrixChar = new char[size[0], size[1]];
  12.  
  13.             for (int i = 0; i < matrixChar.GetLength(0); i++)
  14.             {
  15.                 char[] rowInput = Console.ReadLine().Split(' ',StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
  16.  
  17.                 for (int j = 0; j < matrixChar.GetLength(1); j++)
  18.                 {
  19.                     matrixChar[i, j] = rowInput[j];
  20.                 }
  21.             }
  22.  
  23.             int countSquares2x2 = 0;
  24.             for (int i = 0; i < matrixChar.GetLength(0) - 1; i++)
  25.             {
  26.                 for (int j = 0; j < matrixChar.GetLength(1) - 1; j++)
  27.                 {
  28.                     if (matrixChar[i, j] == matrixChar[i, j + 1] &&
  29.                         matrixChar[i, j] == matrixChar[i + 1, j + 1] &&
  30.                         matrixChar[i, j] == matrixChar[i + 1, j])
  31.                     {
  32.                         countSquares2x2++;
  33.                     }
  34.                 }
  35.             }
  36.  
  37.             Console.WriteLine(countSquares2x2);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement