Advertisement
gosharski

Navigation

Jan 10th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Numerics;
  4.  
  5. namespace Navigation
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int r = int.Parse(Console.ReadLine());
  12. int c = int.Parse(Console.ReadLine());
  13. int n = int.Parse(Console.ReadLine());
  14. int[] codes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  15. BigInteger[,] matrix = new BigInteger[r, c];
  16. InitializeMatrix(matrix, r, c);
  17. //PrintMatrix(matrix, r, c);
  18. int coeff = Math.Max(r, c);
  19. BigInteger sum = 0;
  20. int counter = 0;
  21. int[] currentIndex = new int[2] { r -1, 0};
  22. while (counter <= n - 1)
  23. {
  24. sum += Move(sum, matrix, codes, coeff, r, c, counter, currentIndex);
  25. counter++;
  26. }
  27. Console.WriteLine(sum);
  28. }
  29. static int[] RowCol (int coeff, int[] codes, int counter)
  30. {
  31. int[] coordinates = new int[2];
  32. coordinates[0] = codes[counter] / coeff;
  33. coordinates[1] = codes[counter] % coeff;
  34. return coordinates;
  35. }
  36. static BigInteger Right(BigInteger[,] matrix, int[] GoalRowCol, int[] currentIndex)
  37.  
  38. {
  39. BigInteger sum = 0;
  40. while (GoalRowCol[1] >= currentIndex[1])
  41. {
  42.  
  43. sum += matrix[currentIndex[0], currentIndex[1]];
  44. matrix[currentIndex[0], currentIndex[1]] = 0;
  45. currentIndex[1]++;
  46. }
  47. return sum;
  48. }
  49. static BigInteger Left(BigInteger[,] matrix, int[] GoalRowCol, int[] currentIndex)
  50. {
  51. BigInteger sum = 0;
  52. while (GoalRowCol[1] <= currentIndex[1])
  53. {
  54.  
  55. sum += matrix[currentIndex[0], currentIndex[1]];
  56. matrix[currentIndex[0], currentIndex[1]] = 0;
  57. currentIndex[1]--;
  58. }
  59. return sum;
  60. }
  61. static BigInteger Up(BigInteger[,] matrix, int[] GoalRowCol, int[] currentIndex)
  62. {
  63. BigInteger sum = 0;
  64. while (GoalRowCol[0] <= currentIndex[0])
  65. {
  66.  
  67. sum += matrix[currentIndex[0], currentIndex[1]];
  68. matrix[currentIndex[0], currentIndex[1]] = 0;
  69. currentIndex[0]--;
  70. }
  71. return sum;
  72. }
  73. static BigInteger Down(BigInteger[,] matrix, int[] GoalRowCol, int[] currentIndex)
  74. {
  75. BigInteger sum = 0;
  76. while (GoalRowCol[0] >= currentIndex[0])
  77. {
  78.  
  79. sum += matrix[currentIndex[0], currentIndex[1]];
  80. matrix[currentIndex[0], currentIndex[1]] = 0;
  81. currentIndex[0]++;
  82. }
  83. return sum;
  84. }
  85. static BigInteger Move(BigInteger sum, BigInteger[,] matrix, int[] codes, int coeff, int r, int c, int counter, int[] currentIndex)
  86. {
  87. int[] GoalRowCol = RowCol(coeff, codes, counter);
  88. sum = 0;
  89. if (GoalRowCol[1] > currentIndex[1])
  90. {
  91. sum += Right(matrix, GoalRowCol, currentIndex);
  92. currentIndex[1] = GoalRowCol[1];
  93. }
  94. else if (GoalRowCol[1] < currentIndex[1])
  95. {
  96. sum += Left(matrix, GoalRowCol, currentIndex);
  97. currentIndex[1] = GoalRowCol[1];
  98. }
  99. if (GoalRowCol[1] == currentIndex[1])
  100. {
  101.  
  102. if (GoalRowCol[0] < currentIndex[0])
  103. {
  104. sum += Up(matrix, GoalRowCol, currentIndex);
  105. currentIndex[0] = GoalRowCol[0];
  106. }
  107. else
  108. {
  109. sum += Down(matrix, GoalRowCol, currentIndex);
  110. currentIndex[0] = GoalRowCol[0];
  111. }
  112. }
  113. return sum;
  114. }
  115.  
  116.  
  117.  
  118. //static void PrintMatrix(BigInteger[,] matrix, int r, int c)
  119. //{
  120. // for (int i = 0; i < r; i++)
  121. // {
  122. // for (int j = 0; j < c; j++)
  123. // {
  124. // Console.Write("{0, 4}", matrix[i, j]);
  125. // }
  126. // Console.WriteLine();
  127. // }
  128. //}
  129. static void InitializeMatrix(BigInteger[,] matrix, int r, int c)
  130. {
  131. int a = 0;
  132. for (int row = r - 1; row >= 0; row--)
  133. {
  134.  
  135. for (int col = 0; col < c; col++)
  136. {
  137. matrix[row, col] = BigInteger.Pow(2, a + col);
  138. }
  139. a++;
  140. }
  141. }
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement