Advertisement
Guest User

Square In Matrix

a guest
Jan 24th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. namespace SquareMatrix
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Collections.Generic;
  6.  
  7.  
  8.     class SquaresInMatrix
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // fill matrix - input
  13.             string[,] matrix = FillMatrix();
  14.  
  15.             // proccess - finding squares
  16.             var quares = SearchAboutSquares(matrix);
  17.  
  18.             //output - print count of squares
  19.             PrintCountOfFoundSquares(quares);
  20.         }
  21.  
  22.         private static void PrintCountOfFoundSquares(int squares)
  23.         {
  24.             Console.WriteLine(squares);
  25.         }
  26.  
  27.         private static int SearchAboutSquares(string[,] matrix)
  28.         {
  29.             int cnt = 0;
  30.             for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  31.             {
  32.                 for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  33.                 {
  34.                     if (matrix[row,col] == matrix[row,col +1] &&    // Trying to find
  35.                         matrix[row,col] == matrix[row+1,col] &&     // squares of chars
  36.                         matrix[row,col] == matrix[row+1,col+1])     // in matrix.
  37.                     {
  38.                         cnt++;
  39.                     }
  40.                 }                
  41.             }
  42.  
  43.             return cnt;
  44.         }                        
  45.  
  46.         public static string[,] FillMatrix()
  47.         {
  48.             int[] sizeOf = Console.ReadLine() // size of matrix
  49.                     .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  50.                     .Select(int.Parse)
  51.                     .ToArray();
  52.  
  53.             var matrix = new string[sizeOf[0], sizeOf[1]];
  54.  
  55.             for (int row = 0; row < matrix.GetLength(0); row++)
  56.             {
  57.                 var input = Console.ReadLine() // input for matix
  58.                     .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  59.                     .ToArray();
  60.  
  61.                 for (int col = 0; col < matrix.GetLength(1); col++)
  62.                 {
  63.                     matrix[row, col] = input[col];
  64.                 }
  65.             }
  66.  
  67.             return matrix;
  68.         }
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement