Advertisement
milislavski

LoverOf3

May 8th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Problem3
  8. {
  9.     class LoverOf3
  10.     {
  11.         static void Main()
  12.         {
  13.             var rowsAndCols = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  14.             int rows = int.Parse(rowsAndCols[0]);
  15.             int cols = int.Parse(rowsAndCols[1]);
  16.  
  17.             int[,] matrix = FillMatrix(rows, cols);
  18.  
  19.             int numberOfDirections = int.Parse(Console.ReadLine());
  20.             int currentRow = matrix.GetLength(0) - 1;
  21.             int currentCol = 0;
  22.             long sum = 0;
  23.  
  24.             for (int i = 0; i < numberOfDirections; i++)
  25.             {
  26.                 var directionAndStep = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  27.                 string currentDirection = directionAndStep[0];
  28.                 int currentStep = int.Parse(directionAndStep[1]);
  29.  
  30.                 if (currentDirection == "RU" || currentDirection == "UR")
  31.                 {
  32.                     for (int j = 0; j < currentStep-1; j++)
  33.                     {
  34.  
  35.                         currentRow--;
  36.                         currentCol++;
  37.  
  38.                         if (currentRow < 0 || currentRow >= matrix.GetLength(0)
  39.                            || currentCol < 0 || currentCol >= matrix.GetLength(1))
  40.                         {
  41.                             if (currentRow < 0)
  42.                             {
  43.                                 currentRow++;
  44.                                 currentCol--;
  45.                                 break;
  46.                             }
  47.                             if (currentCol >= matrix.GetLength(1))
  48.                             {
  49.                                 currentCol--;
  50.                                 currentRow++;
  51.                                 break;
  52.                             }
  53.                         }
  54.                         else
  55.                         {
  56.                             int number = matrix[currentRow, currentCol];
  57.                             sum += number;
  58.  
  59.                         }
  60.                         matrix[currentRow, currentCol] = 0;
  61.                     }
  62.                 }
  63.  
  64.                 else if (currentDirection == "LU" || currentDirection == "UL")
  65.                 {
  66.                     for (int j = 0; j < currentStep-1; j++)
  67.                     {
  68.  
  69.                         currentRow--;
  70.                         currentCol--;
  71.  
  72.                         if (currentRow < 0 || currentRow >= matrix.GetLength(0)
  73.                            || currentCol < 0 || currentCol >= matrix.GetLength(1))
  74.                         {
  75.                             if (currentRow < 0)
  76.                             {
  77.                                 currentRow++;
  78.                                 currentCol++;
  79.  
  80.                                 break;
  81.  
  82.                             }
  83.                             if (currentCol < 0)
  84.                             {
  85.                                 currentCol++;
  86.                                 currentRow++;
  87.  
  88.                                 break;
  89.  
  90.                             }
  91.                         }
  92.  
  93.                         else
  94.                         {
  95.                             int number = matrix[currentRow, currentCol];
  96.                             sum += number;
  97.                         }
  98.                         matrix[currentRow, currentCol] = 0;
  99.                     }
  100.                 }
  101.  
  102.                 else if (currentDirection == "DL" || currentDirection == "LD")
  103.                 {
  104.  
  105.                     for (int j = 0; j < currentStep - 1; j++)
  106.                     {
  107.  
  108.                         currentRow++;
  109.                         currentCol--;
  110.  
  111.                         if (currentRow < 0 || currentRow >= matrix.GetLength(0)
  112.                            || currentCol < 0 || currentCol >= matrix.GetLength(1))
  113.                         {
  114.                             if (currentRow >= matrix.GetLength(0))
  115.                             {
  116.                                 currentRow--;
  117.                                 currentCol++;
  118.  
  119.                                 break;
  120.  
  121.                             }
  122.                             if (currentCol < 0)
  123.                             {
  124.                                 currentCol++;
  125.                                 currentRow--;
  126.  
  127.                                 break;
  128.  
  129.                             }
  130.                         }
  131.                         else
  132.                         {
  133.                             int number = matrix[currentRow, currentCol];
  134.                             sum += number;
  135.                             //sum += matrix[currentRow, currentCol];
  136.  
  137.                         }
  138.                         matrix[currentRow, currentCol] = 0;
  139.                     }
  140.                 }
  141.  
  142.                 else if (currentDirection == "DR" || currentDirection == "RD")
  143.                 {
  144.                     for (int j = 0; j < currentStep - 1; j++)
  145.                     {
  146.  
  147.                         currentRow++;
  148.                         currentCol++;
  149.  
  150.  
  151.                         if (currentRow < 0 || currentRow >= matrix.GetLength(0)
  152.                            || currentCol < 0 || currentCol >= matrix.GetLength(1))
  153.                         {
  154.                             if (currentRow >= matrix.GetLength(0))
  155.                             {
  156.                                 currentRow--;
  157.                                 currentCol--;
  158.  
  159.                                 break;
  160.  
  161.                             }
  162.                             if (currentCol >= matrix.GetLength(1))
  163.                             {
  164.                                 currentCol--;
  165.                                 currentRow--;
  166.  
  167.                                 break;
  168.  
  169.                             }
  170.                         }
  171.                         else
  172.                         {
  173.                             int number = matrix[currentRow, currentCol];
  174.                             sum += number;
  175.                         }
  176.                         matrix[currentRow, currentCol] = 0;
  177.  
  178.                     }
  179.                 }
  180.             }
  181.  
  182.             Console.WriteLine(sum);
  183.  
  184.         }
  185.  
  186.         static int[,] FillMatrix(int rows, int cols)
  187.         {
  188.             int[,] matrix = new int[rows, cols];
  189.             int startRowsNumber = 0;
  190.  
  191.             for (int row = matrix.GetLength(0) - 1; row >= 0; row--)
  192.             {
  193.                 int number = startRowsNumber;
  194.                 for (int col = 0; col < matrix.GetLength(1); col++)
  195.                 {
  196.                     matrix[row, col] = number;
  197.                     number += 3;
  198.                 }
  199.                 startRowsNumber += 3;
  200.             }
  201.             return matrix;
  202.         }
  203.  
  204.         static void PrintMatrix(int[,] matrix)
  205.         {
  206.             for (int row = 0; row < matrix.GetLength(0); row++)
  207.             {
  208.                 for (int col = 0; col < matrix.GetLength(1); col++)
  209.                 {
  210.                     Console.Write("{0} ", matrix[row, col]);
  211.                 }
  212.                 Console.WriteLine();
  213.             }
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement