Advertisement
GeorgiGG

Untitled

Jan 19th, 2022
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02.SquaresInMatrix
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] dims = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             int rows = dims[0];
  12.             int cols = dims[1];
  13.             int counter = 0;
  14.             string[,] matrix = ReadStringMatrix(rows, cols);
  15.             string[,] subMatrix = new string[2, 2];
  16.             if (rows < 2 || cols < 2)
  17.             {
  18.                 Console.WriteLine("0");
  19.             }
  20.             else
  21.             {
  22.                 for (int row = 0; row < rows - 1; row++)
  23.                 {
  24.                     for (int col = 0; col < cols - 1; col++)
  25.                     {
  26.                         if (row + 2 < matrix.GetLength(0) && col + 2 < matrix.GetLength(1))
  27.                         {
  28.                             subMatrix[0, 0] = matrix[row, col];
  29.                             subMatrix[0, 1] = matrix[row, col + 1];
  30.                             subMatrix[1, 0] = matrix[row + 1, col];
  31.                             subMatrix[1, 1] = matrix[row + 1, col + 1];
  32.                             if (IsAllSameValues(subMatrix))
  33.                                 counter++;
  34.                         }
  35.                     }
  36.                 }
  37.                 Console.WriteLine($"{counter}");
  38.             }
  39.         }
  40.  
  41.         static string[,] ReadStringMatrix(int rows, int cols)
  42.         {
  43.             string[,] stringMatrix = new string[rows, cols];
  44.  
  45.             for (int row = 0; row < rows; row++)
  46.             {
  47.                 string[] rowInput = Console.ReadLine().Split().ToArray();
  48.  
  49.                 for (int col = 0; col < cols; col++)
  50.                 {
  51.                     stringMatrix[row, col] = rowInput[col];
  52.                 }
  53.             }
  54.             return stringMatrix;
  55.         }
  56.  
  57.         static bool IsAllSameValues(string[,] matrix)
  58.         {
  59.             return true;
  60.             //string value = matrix[0,0];
  61.             for (int row = 0; row < matrix.GetLength(0); row++)
  62.             {
  63.                 for (int col = 0; col < matrix.GetLength(1); col++)
  64.                 {
  65.                     if (matrix[row, col] != matrix[0, 0])
  66.                     {
  67.                         return false;
  68.                         break;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement