nassss

Untitled

Jan 16th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _9._MatrixMaxSum
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int n = int.Parse(Console.ReadLine());
  12. string[] input = Console.ReadLine().Split(' ');
  13. int[,] matrix = new int[n, input.Length];
  14. int sum = 0;
  15. int maxSum = int.MinValue;
  16.  
  17. for (int row = 0; row < n; row++)
  18. {
  19. for (int col = 0; col < input.Length; col++)
  20. {
  21. matrix[row, col] = int.Parse(input[col]);
  22. }
  23. if (row != n - 1)
  24. {
  25. input = Console.ReadLine().Split(' ');
  26. }
  27. }
  28.  
  29. int[] coordinates = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  30.  
  31. for (int i = 0; i < coordinates.Length; i+=2)
  32. {
  33. if (coordinates[i] > 0)
  34. {
  35. for (int col = 0; col < Math.Abs(coordinates[i + 1]); col++)
  36. {
  37. sum += matrix[coordinates[i] - 1, col];
  38. }
  39.  
  40. }
  41. else
  42. {
  43. for (int col = input.Length - 1; col >= Math.Abs(coordinates[i + 1]) - 1; col--)
  44. {
  45. sum += matrix[Math.Abs(coordinates[i]) - 1, col];
  46. }
  47. }
  48. if (coordinates[i + 1] > 0)
  49. {
  50. for (int row = Math.Abs(coordinates[i]) - 2; row >= 0; row--)
  51. {
  52. sum += matrix[row, coordinates[i + 1] - 1];
  53. }
  54. }
  55. else
  56. {
  57. for (int row = Math.Abs(coordinates[i]); row < n; row++)
  58. {
  59. sum += matrix[row, Math.Abs(coordinates[i + 1]) - 1];
  60. }
  61. }
  62. if (sum > maxSum)
  63. {
  64. maxSum = sum;
  65. }
  66. sum = 0;
  67. }
  68. Console.WriteLine(maxSum);
  69. }
  70.  
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment