Advertisement
Bat3Sa

2X2SquaresInMatrix

May 25th, 2020
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02_SquaresInMatrix
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             var sizes = Console.ReadLine()
  11.                 .Split()
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.  
  15.             var rows = sizes[0];
  16.  
  17.             var cols = sizes[1];
  18.  
  19.             var matrix = new char[rows, cols];
  20.  
  21.             var numOfSquares = 0;
  22.  
  23.             for (int r = 0; r < matrix.GetLength(0); r++)
  24.             {
  25.                 var chars = Console.ReadLine()
  26.                     .Split()
  27.                     .Select(char.Parse)
  28.                     .ToArray();
  29.  
  30.                 for (int c = 0; c < matrix.GetLength(1); c++)
  31.                 {
  32.                     matrix[r, c] = chars[c];
  33.                 }
  34.             }
  35.  
  36.             for (int r = 0; r < matrix.GetLength(0) - 1; r++)  
  37.             {
  38.                 for (int c = 0; c < matrix.GetLength(1) - 1; c++)
  39.                 {
  40.                     if (matrix[r, c] == matrix[r, c + 1]
  41.                         && matrix[r, c] == matrix[r + 1, c]
  42.                         && matrix[r, c] == matrix[r + 1, c + 1])
  43.                     {
  44.                         numOfSquares++;
  45.                     }
  46.                 }
  47.             }
  48.             Console.WriteLine(numOfSquares);
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement