Advertisement
vaakata

Hourglass Sum 1.06.2016

Jun 1st, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace HourGlassSum_31._05._2016
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. decimal[,] inputMatrix = new decimal[6, 6];
  14. inputMatrix = (ReadingInput());
  15.  
  16. decimal maxHourglasSum = (FindingMaxHourglassSum(inputMatrix));
  17. Console.WriteLine(maxHourglasSum);
  18. }
  19.  
  20. static decimal FindingMaxHourglassSum(decimal[,] scannedMatrix)
  21. {
  22. decimal maxHGlassSum = 0;
  23. decimal currentHGlassSum = 0;
  24.  
  25. for (int rowScan = 0; rowScan < scannedMatrix.GetLength(0) - 2; rowScan++)
  26. {
  27. for (int colScan = 0; colScan < scannedMatrix.GetLength(1) - 2; colScan++)
  28. {
  29. currentHGlassSum = SumCurrentHGlass(scannedMatrix, rowScan, colScan); // sum items of current "Hourglass matrix" with start indexes - current values of rowScan, colScan
  30. if (currentHGlassSum > maxHGlassSum)
  31. {
  32. maxHGlassSum = currentHGlassSum;
  33. }
  34. }
  35. }
  36. return maxHGlassSum;
  37. }
  38.  
  39. static decimal SumCurrentHGlass(decimal[,] sumProcessMatrix, int rowScan, int colScan)
  40. {
  41. decimal sumHG = 0; // the sum of current Hourglass shaped matrix
  42. for (int rowSum = rowScan; rowSum < rowScan + 3; rowSum++)
  43. {
  44. for (int colSum = colScan; colSum < colScan + 3; colSum++)
  45. {
  46. if (rowSum == rowScan + 1 && (colSum == colScan || colSum == colScan +2)) // skipping middle row's first and last elements
  47. {
  48. continue;
  49. }
  50. else
  51. {
  52. sumHG += sumProcessMatrix[rowSum, colSum];
  53. }
  54. }
  55. }
  56. return sumHG;
  57. }
  58.  
  59. static decimal[,] ReadingInput(int size = 6)
  60. {
  61. decimal[,] readMatrix = new decimal[size, size];
  62.  
  63. for (int rowRead = 0; rowRead < size; rowRead++)
  64. {
  65. decimal[] inputRow = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(decimal.Parse).ToArray();
  66.  
  67. for (int colRead = 0; colRead < size; colRead++)
  68. {
  69. readMatrix[rowRead, colRead] = inputRow[colRead];
  70. }
  71. }
  72. return readMatrix;
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement