yanchevilian

02. 2x2 Squares in Matrix from C# Advanced

Jul 19th, 2021 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _2X2_Squares_in_Matrix
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] matrixSizes = Console.ReadLine()
  11.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.  
  15.             int rows = matrixSizes[0];
  16.             int cols = matrixSizes[1];
  17.  
  18.             char[,] matrix = ReadMatrix(rows, cols);
  19.             int counter = 0;
  20.  
  21.             for (int rowIndex = 0; rowIndex < rows - 1; rowIndex++)
  22.             {
  23.                 for (int colIndex = 0; colIndex < cols - 1; colIndex++)
  24.                 {
  25.                     if (matrix[rowIndex, colIndex] == matrix[rowIndex, colIndex + 1] &&
  26.                         matrix[rowIndex, colIndex] == matrix[rowIndex + 1, colIndex] &&
  27.                         matrix[rowIndex, colIndex] == matrix[rowIndex + 1, colIndex + 1])
  28.                     {
  29.                         counter++;
  30.                     }
  31.                 }
  32.             }
  33.  
  34.             Console.WriteLine(counter);
  35.         }
  36.         static char[,] ReadMatrix(int rows, int cols)
  37.         {
  38.             char[,] matrix = new char[rows, cols];
  39.  
  40.  
  41.             for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  42.             {
  43.                 char[] inputChars = Console.ReadLine()
  44.                     .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  45.                     .Select(char.Parse)
  46.                     .ToArray();
  47.                 for (int colIndex = 0; colIndex < cols; colIndex++)
  48.                 {
  49.                     matrix[rowIndex, colIndex] = inputChars[colIndex];
  50.                 }
  51.             }
  52.  
  53.             return matrix;
  54.         }
  55.     }
  56. }
Add Comment
Please, Sign In to add comment