Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. namespace JediGalaxy
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Numerics;
  7.  
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. var dimensions = ParseInput(Console.ReadLine());
  13.  
  14. var rows = dimensions[0];
  15. var cols = dimensions[1];
  16.  
  17. var matrix = new int[rows][];
  18. FillMatrix(matrix,rows,cols);
  19.  
  20. var input = Console.ReadLine();
  21. BigInteger collectedStarsSum = 0;
  22. var starsHittedByEvil = new Dictionary<int, HashSet<int>>();
  23.  
  24. while (input != "Let the Force be with you")
  25. {
  26. var ivoCoordinates = ParseInput(input);
  27. var evilCoordinates = ParseInput(Console.ReadLine());
  28.  
  29. CollectStars(matrix, ivoCoordinates, evilCoordinates, ref collectedStarsSum, starsHittedByEvil);
  30.  
  31. input = Console.ReadLine();
  32. }
  33.  
  34. Console.WriteLine(collectedStarsSum);
  35. }
  36.  
  37. private static void CollectStars(int[][] matrix, int[] ivoCoordinates, int[] evilCoordinates, ref BigInteger collectedStarsSum, Dictionary<int, HashSet<int>> starsHittedByEvil)
  38. {
  39. var ivoRow = ivoCoordinates[0];
  40. var ivoCol = ivoCoordinates[1];
  41.  
  42. var evilRow = evilCoordinates[0];
  43. var evilCol = evilCoordinates[1];
  44.  
  45. HitStars(matrix, evilRow, evilCol, starsHittedByEvil);
  46.  
  47. for (int r = ivoRow; r >= 0; r--)
  48. {
  49. if (ivoCol < 0 || r >= matrix.Length)
  50. {
  51. ivoCol++;
  52. continue;
  53. }
  54.  
  55. if (ivoCol >= matrix[0].Length)
  56. {
  57. break;
  58. }
  59.  
  60. if (!starsHittedByEvil.ContainsKey(r) || !starsHittedByEvil[r].Contains(ivoCol))
  61. {
  62. collectedStarsSum += matrix[r][ivoCol];
  63. }
  64.  
  65. ivoCol++;
  66. }
  67. }
  68.  
  69. private static void HitStars(int[][] matrix, int evilRow, int evilCol, Dictionary<int, HashSet<int>> starsHittedByEvil)
  70. {
  71. for (int r = evilRow; r >= 0; r--)
  72. {
  73. if (evilCol >= matrix[0].Length || (r >= matrix.Length))
  74. {
  75. evilCol--;
  76. continue;
  77. }
  78.  
  79. if (evilCol < 0)
  80. {
  81. break;
  82. }
  83.  
  84. if (!starsHittedByEvil.ContainsKey(r))
  85. {
  86. starsHittedByEvil.Add(r, new HashSet<int>());
  87. }
  88. starsHittedByEvil[r].Add(evilCol);
  89.  
  90. evilCol--;
  91. }
  92. }
  93.  
  94. private static void FillMatrix(int[][] matrix, int rows, int cols)
  95. {
  96. int num = 0;
  97.  
  98. for (int r = 0; r < rows; r++)
  99. {
  100. matrix[r] = new int[cols];
  101. for (int c = 0; c < cols; c++)
  102. {
  103. matrix[r][c] = num++;
  104. }
  105. }
  106. }
  107.  
  108. private static int[] ParseInput(string input)
  109. {
  110. return input
  111. .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  112. .Select(int.Parse)
  113. .ToArray();
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement