Advertisement
Guest User

Jedi Galaxy

a guest
Jun 16th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.00 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02.JediGalaxy
  4. {
  5.     class JediGalaxy
  6.     {
  7.         static long totalSum = 0;
  8.  
  9.         static void Main(string[] args)
  10.         {
  11.             string[] input = Console.ReadLine().Split();
  12.             int rows = int.Parse(input[0]);
  13.             int cols = int.Parse(input[1]);
  14.             int[,] matrix = new int[rows, cols];
  15.  
  16.             //Init matrix
  17.             for (int row = 0; row < rows; row++)
  18.             {
  19.                 for (int col = 0; col < cols; col++)
  20.                 {
  21.                     matrix[row, col] = rows * row + col;
  22.                 }
  23.             }
  24.  
  25.             int iterationCounter = 0;
  26.             bool isGood = false;
  27.             bool isEvil = false;
  28.             int goodRow = 0;
  29.             int goodCol = 0;
  30.             int goodDiagonal = 0;
  31.             int evilRow = 0;
  32.             int evilCol = 0;
  33.             int evilDiagonal = 0;
  34.  
  35.             string command;
  36.             while ((command = Console.ReadLine()) != "Let the Force be with you")
  37.             {
  38.                 //If iteration number is even we read Ivo's row and column and also taka the current diagonal.
  39.                 //Left diagonals (Ivo's diagonals or Good diagonals) of the matrix are calculated by
  40.                 //current row + current column. The result should be a number between 0 and max valid row + max valid column
  41.                 //otherwise diagonal does not hit the matrix.
  42.                 //If iteration number is odd we read Evil row and column.
  43.                 //Right diagonals or Evil diagonal are calculated by current column - current row. The range of the result
  44.                 //must be from negative value of max valid row to max valid column.
  45.                 if (iterationCounter % 2 == 0)
  46.                 {
  47.                     goodRow = int.Parse(command.Split()[0]);
  48.                     goodCol = int.Parse(command.Split()[1]);
  49.                     goodDiagonal = goodRow + goodCol;
  50.                     isGood = true;
  51.                 }
  52.                 else
  53.                 {
  54.                     evilRow = int.Parse(command.Split()[0]);
  55.                     evilCol = int.Parse(command.Split()[1]);
  56.                     evilDiagonal = evilCol - evilRow;
  57.                     isEvil = true;
  58.                 }
  59.  
  60.                 //If the both inputs are read we are ready to continue with the tough part.
  61.                 if (isEvil && isGood)
  62.                 {
  63.                     //Boolean variables to check if given diagonal hit the matrix.
  64.                     bool isEvilDiagonalInTheMatrix = evilDiagonal < cols && evilDiagonal > (rows * -1);
  65.                     bool isGoodDiagonalInTheMatrix = goodDiagonal <= rows + cols - 2 && goodDiagonal >= 0;
  66.  
  67.                     if (isEvilDiagonalInTheMatrix && isGoodDiagonalInTheMatrix)
  68.                     {
  69.                         DestroyDiagonal(matrix, rows, cols, evilDiagonal);
  70.                         CollectDiagonal(matrix, rows, cols, goodDiagonal);
  71.                     }
  72.                     else if (isEvilDiagonalInTheMatrix || isGoodDiagonalInTheMatrix)
  73.                     {
  74.                         if (isEvilDiagonalInTheMatrix)
  75.                             DestroyDiagonal(matrix, rows, cols, evilDiagonal);
  76.                         else
  77.                             CollectDiagonal(matrix, rows, cols, goodDiagonal);
  78.                     }
  79.  
  80.                     isEvil = false;
  81.                     isGood = false;
  82.                 }
  83.  
  84.                 iterationCounter++;
  85.             }
  86.  
  87.             Console.WriteLine(totalSum);
  88.         }
  89.  
  90.         //Ivo's diagonal or Good diagonal
  91.         //Method thath search for first cell in the diagonal that is in tha matrix and
  92.         //then collect the value from every cell in that diagonal
  93.         private static void CollectDiagonal(int[,] matrix, int rows, int cols, int goodDiagonal)
  94.         {
  95.             int currentRow = rows;
  96.             int currentCol = -1;
  97.  
  98.             while (currentRow >= rows)
  99.             {
  100.                 currentCol++;
  101.                 currentRow = goodDiagonal - currentCol;
  102.             }
  103.  
  104.             while (currentCol < cols && currentRow >= 0)
  105.             {
  106.                 totalSum += matrix[currentRow, currentCol];
  107.                 currentCol++;
  108.                 currentRow--;
  109.             }
  110.         }
  111.  
  112.         //Evil diagonal
  113.         //Method thath search for first cell in the diagonal that is in tha matrix and
  114.         //then detroys every cell in that diagonal
  115.         private static void DestroyDiagonal(int[,] matrix, int rows, int cols, int evilDiagonal)
  116.         {
  117.             int currentRow = rows;
  118.             int currentCol = cols;
  119.  
  120.             while (currentRow >= rows)
  121.             {
  122.                 currentCol--;
  123.                 currentRow = currentCol - evilDiagonal;
  124.             }
  125.  
  126.             while (currentCol >= 0 && currentRow >= 0)
  127.             {
  128.                 matrix[currentRow, currentCol] = 0;
  129.                 currentCol--;
  130.                 currentRow--;
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement