Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _02.SquaresInMatrix
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int[] dims = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int rows = dims[0];
- int cols = dims[1];
- int counter = 0;
- string[,] matrix = ReadStringMatrix(rows, cols);
- string[,] subMatrix = new string[2, 2];
- if (rows < 2 || cols < 2)
- {
- Console.WriteLine("0");
- }
- else
- {
- for (int row = 0; row < rows - 1; row++)
- {
- for (int col = 0; col < cols - 1; col++)
- {
- if (row + 2 < matrix.GetLength(0) && col + 2 < matrix.GetLength(1))
- {
- subMatrix[0, 0] = matrix[row, col];
- subMatrix[0, 1] = matrix[row, col + 1];
- subMatrix[1, 0] = matrix[row + 1, col];
- subMatrix[1, 1] = matrix[row + 1, col + 1];
- if (IsAllSameValues(subMatrix))
- counter++;
- }
- }
- }
- Console.WriteLine($"{counter}");
- }
- }
- static string[,] ReadStringMatrix(int rows, int cols)
- {
- string[,] stringMatrix = new string[rows, cols];
- for (int row = 0; row < rows; row++)
- {
- string[] rowInput = Console.ReadLine().Split().ToArray();
- for (int col = 0; col < cols; col++)
- {
- stringMatrix[row, col] = rowInput[col];
- }
- }
- return stringMatrix;
- }
- static bool IsAllSameValues(string[,] matrix)
- {
- return true;
- //string value = matrix[0,0];
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (matrix[row, col] != matrix[0, 0])
- {
- return false;
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement