EvgeniVT

Squares in Matrix

Sep 24th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Squares_in_Matrix
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             var nxm = Console.ReadLine()
  11.                 .Split()
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.             var rows = nxm[0];
  15.             var cols = nxm[1];
  16.             var count = 0;
  17.             if (rows >= 2 && cols >= 2)
  18.             {
  19.                 var matrix = new char[rows, cols];
  20.                 for (int row = 0; row < rows; row++)
  21.                 {
  22.                     var input = Console.ReadLine()
  23.                         .Split(' ',StringSplitOptions.RemoveEmptyEntries)
  24.                         .Select(char.Parse)
  25.                         .ToArray();
  26.                     for (int col = 0; col < cols; col++)
  27.                     {
  28.                         matrix[row, col] = input[col];
  29.                     }
  30.                 }
  31.                 for (int row = 0; row < rows - 1; row++)
  32.                 {
  33.                     for (int col = 0; col < cols - 1; col++)
  34.                     {
  35.                         if (matrix[row, col] == matrix[row, col + 1] &&
  36.                             matrix[row, col] == matrix[row + 1, col + 1] &&
  37.                             matrix[row, col] == matrix[row + 1, col])
  38.                         {
  39.                             count++;
  40.                         }
  41.                     }
  42.                 }
  43.             }
  44.             Console.WriteLine(count);          
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment