Advertisement
Absend

Prob3Lovers

Mar 6th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class ProblemLover3
  6. {
  7.     static void Main()
  8.     {
  9.         // filling matrix
  10.         int[] rowCol = Console.ReadLine()
  11.             .Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  12.             .Select(r => int.Parse(r))
  13.             .ToArray();
  14.  
  15.         int x = rowCol[0];
  16.         int y = rowCol[1];
  17.  
  18.         int[,] matrix = new int[x, y];
  19.  
  20.         int p = 0;
  21.         int k = 3;
  22.         matrix[rowCol[0] - 1, 0] = p;
  23.  
  24.         for (int i = x - 1; i >= 0; i--)
  25.         {
  26.             for (int j = 0; j < y; j++)
  27.             {
  28.                 matrix[i, j] = p;
  29.                 p += 3;
  30.             }
  31.             p = k;
  32.             k += 3;
  33.         }
  34.  
  35.         // reading instruction lines
  36.         int n = int.Parse(Console.ReadLine());
  37.         List<string> lines = new List<string>();
  38.  
  39.         for (int i = 0; i < n; i++)
  40.         {
  41.             lines.Add(Console.ReadLine());
  42.         }
  43.  
  44.         // solution
  45.         int currentX = x - 1;
  46.         int currentY = 0;
  47.         int sum = 0;
  48.  
  49.         for (int i = 0; i < n; i++)
  50.         {
  51.             string direction = lines[i].Substring(0, 2);
  52.             for (int j = 0; j < (lines[i][lines[i].Length - 1] - '0'); j++)
  53.             {
  54.                 sum += matrix[currentX, currentY];
  55.                 matrix[currentX, currentY] = 0;
  56.  
  57.                 if (direction == "RU" || direction == "UR")
  58.                 {
  59.                     currentX--;
  60.                     currentY++;
  61.                     if (currentX < 0)
  62.                     {
  63.                         currentX = 0;
  64.                     }
  65.                     if (currentY >= y)
  66.                     {
  67.                         currentY = y - 1;
  68.                     }
  69.                 }
  70.                 if (direction == "LU" || direction == "UL")
  71.                 {
  72.                     currentX--;
  73.                     currentY--;
  74.                     if (currentX < 0)
  75.                     {
  76.                         currentX = 0;
  77.                     }
  78.                     if (currentY < 0)
  79.                     {
  80.                         currentY = 0;
  81.                     }
  82.                 }
  83.                 if (direction == "DL" || direction == "LD")
  84.                 {
  85.                     currentX++;
  86.                     currentY++;
  87.                     if (currentX >= x)
  88.                     {
  89.                         currentX = x - 1;
  90.                     }
  91.                     if (currentY >= y)
  92.                     {
  93.                         currentY = y - 1;
  94.                     }
  95.                 }
  96.                 if (direction == "RD" || direction == "DR")
  97.                 {
  98.                     currentX++;
  99.                     currentY--;
  100.                     if (currentX >= x)
  101.                     {
  102.                         currentX = x - 1;
  103.                     }
  104.                     {
  105.                         currentY = 0;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         Console.WriteLine(sum);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement