Advertisement
ina_abadjieva

Hourglass Sum

Jun 1st, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 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. using System.Numerics;
  7.  
  8. namespace HourglassSum
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var matrix = new long[6, 6];
  15.  
  16. ReadingMatrix(matrix);
  17. long result = CalculateSum(matrix);
  18. Console.WriteLine(result);
  19. }
  20.  
  21. private static long CalculateSum(long[,] matrix)
  22. {
  23. long maxSum = long.MinValue;
  24. for (int row = 0; row < 4; row++)
  25. {
  26. for (int col = 0; col < 4; col++)
  27. {
  28. long sum = 0;
  29. for (int i = 0; i < 3; i++)
  30. {
  31. for (int j = 0; j < 3; j++)
  32. {
  33. if (row + i == 1 && col + j == 0 ||
  34. row + i == 1 && col + j == 2)
  35. {
  36. continue;
  37. }
  38. else
  39. {
  40. sum += matrix[row + i, col + j];
  41. }
  42. }
  43. }
  44. if (maxSum < sum)
  45. {
  46. maxSum = sum;
  47. }
  48. }
  49. }
  50. return maxSum;
  51. }
  52.  
  53. public static long[,] ReadingMatrix(long[,] matrix)
  54. {
  55. for (int row = 0; row < 6; row++)
  56. {
  57. var cells = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
  58. for (int col = 0; col < 6; col++)
  59. {
  60. matrix[row, col] = cells[col];
  61. }
  62. }
  63. return matrix;
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement