Advertisement
dimipan80

Lover of 3 (matrix traverse)

May 26th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. /* The game has a rectangular field. The field is filled with numbers devisable by 3 as follows: The bottom left corner holds the value 0; The next cell above holds value of 3, the next cell above holds of 6, etc…; The second cell the bottom row holds a value of 3, the cell next to it holds a value of 6.
  2.  * You have a pawn on the field. The pawn can only move to the cells that are diagonally next to it. The possible directions are UP-RIGHT, DOWN-RIGHT, UP-LEFT and DOWN-LEFT.
  3.  * Given that initially the pawn starts at the bottom left corner, a list of directions and how many moves the pawn is about to perform in each direction, calculate the sum of the cells that the pawn has to go through.
  4.  * The value of each cell is calculated only once, i.e. if the pawn visits the same cell more than once, its value is added to the result only the first time (the value is collected).
  5.  * If the pawn is about to perform K moves in the given direction, but there are less than K cells before the edge of the field, the pawn performs as many moves as are available and stops at the edge of the field.
  6.  * On the first input line you will find the dimensions of the field R and C, separated with a single space. On the second line you will find the number N, the number of directions and moves. On the next N lines you will find a string D and a number K, separated with a single space: D is the next direction of the pawn; K is the number of moves to perform in this direction. Print the sum of cells contained in the path of pawn!
  7.  * D will always have one of the values RU, UR, LU, UL, DL, LD, RD or DR: RU and UR mean UP-RIGHT; LU and UL mean UP-LEFT; DL and LD mean DOWN-LEFT; DR and RD mean DOWN-RIGHT. */
  8.  
  9. namespace Lover_of_3
  10. {
  11.     using System;
  12.     using System.Linq;
  13.  
  14.     class LoverOf3
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             int[] sizes = Console.ReadLine().Split()
  19.                 .Select(int.Parse).ToArray();
  20.  
  21.             int n = int.Parse(Console.ReadLine());
  22.             bool[,] matrix = new bool[sizes[0], sizes[1]];
  23.             int sum = 0;
  24.             int row = sizes[0] - 1;
  25.             int col = 0;
  26.             for (int i = 0; i < n; i++)
  27.             {
  28.                 string[] nextMoves = Console.ReadLine().Split();
  29.                 string direction = nextMoves[0];
  30.                 int countMoves = int.Parse(nextMoves[1]);
  31.                 for (int j = 1; j < countMoves; j++)
  32.                 {
  33.                     if (direction == "RU" || direction == "UR")
  34.                     {
  35.                         if (row - 1 >= 0 && row < matrix.GetLength(0) &&
  36.                                 col >= 0 && col + 1 < matrix.GetLength(1))
  37.                         {
  38.                             row--;
  39.                             col++;
  40.                         }
  41.                         else break;
  42.                     }
  43.                     else if (direction == "DR" || direction == "RD")
  44.                     {
  45.                         if (row >= 0 && row + 1 < matrix.GetLength(0) &&
  46.                                 col >= 0 && col + 1 < matrix.GetLength(1))
  47.                         {
  48.                             row++;
  49.                             col++;
  50.                         }
  51.                         else break;
  52.                     }
  53.                     else if (direction == "LU" || direction == "UL")
  54.                     {
  55.                         if (row - 1 >= 0 && row < matrix.GetLength(0) &&
  56.                             col - 1 >= 0 && col < matrix.GetLength(1))
  57.                         {
  58.                             row--;
  59.                             col--;
  60.                         }
  61.                         else break;
  62.                     }
  63.                     else if (direction == "DL" || direction == "LD")
  64.                     {
  65.                         if (row >= 0 && row + 1 < matrix.GetLength(0) &&
  66.                                 col - 1 >= 0 && col < matrix.GetLength(1))
  67.                         {
  68.                             row++;
  69.                             col--;
  70.                         }
  71.                         else break;
  72.                     }
  73.  
  74.                     if (matrix[row, col] == true) continue;
  75.  
  76.                     sum += ((matrix.GetLength(0) - row - 1) * 3 + col * 3);
  77.                     matrix[row, col] = true;
  78.                 }
  79.             }
  80.  
  81.             Console.WriteLine(sum);
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement