nassss

Untitled

Mar 20th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SmallWord
  6. {
  7. class Program
  8. {
  9. static int counter = 0;
  10. static List<int> output = new List<int>();
  11. static char bomb = 'b';
  12. static int currentNum = 0;
  13. static void Main(string[] args)
  14. {
  15.  
  16. var output = LoadingTheMatrix();
  17. var finalOutput = output.OrderByDescending(x => x).ToList();
  18. foreach (var item in finalOutput)
  19. {
  20. Console.WriteLine(item);
  21. }
  22. }
  23. static List<int> LoadingTheMatrix()
  24. {
  25. var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  26. var rows = input[0];
  27. var cols = input[1];
  28. if (rows < 1 || cols > 20)
  29. {
  30. throw new ArgumentOutOfRangeException("Rows and cols should be between following ranges >=1 && <= 20!");
  31. }
  32. int[,] matrix = new int[rows, cols];
  33. for (int i = 0; i < rows; i++)
  34. {
  35. var row = Console.ReadLine();
  36.  
  37. for (int j = 0; j < cols; j++)
  38. {
  39. matrix[i, j] = int.Parse(row[j].ToString());
  40. }
  41. }
  42. for (int k = 0;k < rows;k++)
  43. {
  44. for (int l = 0; l < cols; l++)
  45. {
  46. currentNum = matrix[k, l];
  47. TravelOverTheMatrix(k, l, matrix);
  48. if (counter >0)
  49. {
  50. output.Add(counter);
  51. }
  52. counter = 0;
  53. }
  54. }
  55. return output;
  56. }
  57. static void TravelOverTheMatrix( int row, int col, int [,] matrix)
  58. {
  59. if ((row<0) ||(row >= matrix.GetLength(0)) ||
  60. (col<0) || (col>= matrix.GetLength(1)) ||
  61. matrix[row,col] == bomb)
  62. {
  63. return;
  64. }
  65. if (matrix[row,col] != 1)
  66. {
  67. return;
  68. }
  69. matrix[row, col] = bomb;
  70. counter++;
  71. TravelOverTheMatrix(row, col - 1, matrix);
  72. TravelOverTheMatrix(row-1, col, matrix);
  73. TravelOverTheMatrix(row + 1, col, matrix);
  74. TravelOverTheMatrix(row, col + 1, matrix);
  75.  
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment