Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. namespace _2.Jedi_Galaxy
  2. {
  3. using System;
  4. using System.Linq;
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. var dimensions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  10. var matrixRows = dimensions[0];
  11. var matrixCols = dimensions[1];
  12. var galaxy = new long[matrixRows][];
  13. long stars = 0;
  14. for (int i = 0; i < galaxy.Length; i++)
  15. {
  16. galaxy[i]= new long[matrixCols];
  17. for (int j = 0; j < galaxy[i].Length; j++)
  18. {
  19. galaxy[i][j] = stars;
  20. stars++;
  21. }
  22. }
  23. var ivoCordinates = Console.ReadLine();
  24. int ivoRow = ivoCordinates[0];
  25. int ivoCol = ivoCordinates[1];
  26. long starsCollected = 0;
  27. var evilForces = Console.ReadLine();
  28. while (evilForces!= "Let the Force be with you" || ivoCordinates!= "Let the Force be with you")
  29. {
  30. var ivoTokens = ivoCordinates.Split().Select(int.Parse).ToArray();
  31. ivoRow = ivoTokens[0];
  32. ivoCol = ivoTokens[1];
  33.  
  34. var evilForcesCord = evilForces.Split().Select(int.Parse).ToArray();
  35. var evilForcesRow = evilForcesCord[0];
  36. var evilForcesCol = evilForcesCord[1];
  37.  
  38. EvilForcesContributeToGalaxy(galaxy, evilForcesRow, evilForcesCol, matrixRows,matrixCols);
  39. starsCollected= IvoCollectingStars(galaxy, ivoRow, ivoCol, matrixRows, matrixCols, ivoCordinates);
  40. ivoCordinates = Console.ReadLine();
  41. if (ivoCordinates == "Let the Force be with you")
  42. {
  43. break;
  44. }
  45. evilForces = Console.ReadLine();
  46. }
  47.  
  48. Console.WriteLine(starsCollected);
  49. }
  50.  
  51. private static long IvoCollectingStars(long[][] galaxy, int ivoRow, int ivoCol, int matrixRows, int matrixCols, string ivoCordinates)
  52. {
  53. long starsCollected = 0;
  54. while (ivoRow >= 0 && ivoCol < matrixCols)
  55. {
  56. if (IsInMatrix(matrixRows, ivoRow, ivoCol, matrixCols))
  57. {
  58. starsCollected += galaxy[ivoRow][ivoCol];
  59. }
  60. ivoRow--;
  61. ivoCol++;
  62. }
  63. var ivoTokens = ivoCordinates.Split().Select(int.Parse).ToArray();
  64. ivoRow = ivoTokens[0] + 1;
  65. ivoCol = ivoTokens[1] - 1;
  66. while (ivoRow < matrixRows && ivoCol >= 0)
  67. {
  68. if (IsInMatrix(matrixRows, ivoRow, ivoCol, matrixCols))
  69. {
  70. starsCollected += galaxy[ivoRow][ivoCol];
  71. }
  72. ivoRow++;
  73. ivoCol--;
  74.  
  75. }
  76. return starsCollected;
  77. }
  78.  
  79. private static void EvilForcesContributeToGalaxy(long[][] galaxy, int evilForcesRow, int evilForcesCol, int rows , int cols )
  80. {
  81.  
  82. while (evilForcesRow>=0 && evilForcesCol>=0)
  83. {
  84. if (IsInMatrix(rows,evilForcesRow,evilForcesCol,cols))
  85. {
  86. galaxy[evilForcesRow][evilForcesCol] = 0;
  87. }
  88. evilForcesRow--;
  89. evilForcesCol--;
  90. }
  91. while (evilForcesRow<rows && evilForcesCol < cols)
  92. {
  93. if (IsInMatrix(rows, evilForcesRow, evilForcesCol, cols))
  94. {
  95. galaxy[evilForcesRow][evilForcesCol] = 0;
  96. }
  97.  
  98. evilForcesRow++;
  99. evilForcesCol++;
  100. }
  101. }
  102. public static bool IsInMatrix(int row, int testRow, int testCol, int columns)
  103. {
  104. if (testRow >= 0 && testRow < row && testCol >= 0 && testCol < columns)
  105. {
  106. return true;
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement