Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace SmallWord
- {
- class Program
- {
- static int counter = 0;
- static List<int> output = new List<int>();
- static char bomb = 'b';
- static int currentNum = 0;
- static void Main(string[] args)
- {
- var output = LoadingTheMatrix();
- var finalOutput = output.OrderByDescending(x => x).ToList();
- foreach (var item in finalOutput)
- {
- Console.WriteLine(item);
- }
- }
- static List<int> LoadingTheMatrix()
- {
- var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- var rows = input[0];
- var cols = input[1];
- if (rows < 1 || cols > 20)
- {
- throw new ArgumentOutOfRangeException("Rows and cols should be between following ranges >=1 && <= 20!");
- }
- int[,] matrix = new int[rows, cols];
- for (int i = 0; i < rows; i++)
- {
- var row = Console.ReadLine();
- for (int j = 0; j < cols; j++)
- {
- matrix[i, j] = int.Parse(row[j].ToString());
- }
- }
- for (int k = 0;k < rows;k++)
- {
- for (int l = 0; l < cols; l++)
- {
- currentNum = matrix[k, l];
- TravelOverTheMatrix(k, l, matrix);
- if (counter >0)
- {
- output.Add(counter);
- }
- counter = 0;
- }
- }
- return output;
- }
- static void TravelOverTheMatrix( int row, int col, int [,] matrix)
- {
- if ((row<0) ||(row >= matrix.GetLength(0)) ||
- (col<0) || (col>= matrix.GetLength(1)) ||
- matrix[row,col] == bomb)
- {
- return;
- }
- if (matrix[row,col] != 1)
- {
- return;
- }
- matrix[row, col] = bomb;
- counter++;
- TravelOverTheMatrix(row, col - 1, matrix);
- TravelOverTheMatrix(row-1, col, matrix);
- TravelOverTheMatrix(row + 1, col, matrix);
- TravelOverTheMatrix(row, col + 1, matrix);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment