gabi11

Multidimensional Arrays - 02. Squares in Matrix

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