zaryana

PresentDelivery

Oct 16th, 2020 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.33 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02_PresentDelivery
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int presentsCount = int.Parse(Console.ReadLine());
  10.             int size = int.Parse(Console.ReadLine());
  11.  
  12.             string[,] matrix = new string[size, size];
  13.  
  14.             int santaRow = -1;
  15.             int santaCol = -1;
  16.  
  17.             int goodKids = 0;
  18.  
  19.             for (int row = 0; row < matrix.GetLength(0); row++)
  20.             {
  21.                 string[] currRow = Console.ReadLine()
  22.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  23.  
  24.                 for (int col = 0; col < matrix.GetLength(1); col++)
  25.                 {
  26.                     if (currRow[col].ToString() == "S")
  27.                     {
  28.                         santaRow = row;
  29.                         santaCol = col;
  30.                     }
  31.                     else if (currRow[col].ToString() == "V")
  32.                     {
  33.                         goodKids++;
  34.                     }
  35.  
  36.                     matrix[row, col] = currRow[col];
  37.                 }
  38.             }
  39.  
  40.             string command = "";
  41.  
  42.             while ((command = Console.ReadLine()) != "Christmas morning" && presentsCount > 0)
  43.             {
  44.                 matrix[santaRow, santaCol] = "-";
  45.  
  46.                 if (command == "up")
  47.                 {
  48.                     santaRow--;
  49.                 }
  50.                 else if (command == "down")
  51.                 {
  52.                     santaRow++;
  53.                 }
  54.                 else if (command == "left")
  55.                 {
  56.                     santaCol--;
  57.                 }
  58.                 else if (command == "right")
  59.                 {
  60.                     santaCol++;
  61.                 }
  62.  
  63.                 if (CheckBounderies(matrix, santaRow, santaCol))
  64.                 {
  65.                     Console.WriteLine("Santa ran out of presents.");
  66.                     break;
  67.                 }
  68.  
  69.                 if (matrix[santaRow, santaCol] == "-")
  70.                 {
  71.                     matrix[santaRow, santaCol] = "S";
  72.                 }
  73.                 else if (matrix[santaRow, santaCol] == "V")
  74.                 {
  75.                     matrix[santaRow, santaCol] = "S";
  76.                     presentsCount--;
  77.  
  78.                     if (presentsCount == 0)
  79.                     {
  80.                         Console.WriteLine("Santa ran out of presents!");
  81.                         break;
  82.                     }
  83.                 }
  84.                 else if (matrix[santaRow, santaCol] == "X")
  85.                 {
  86.                     continue;
  87.                 }
  88.                 else if (matrix[santaRow, santaCol] == "C")
  89.                 {
  90.                     matrix[santaRow, santaCol] = "S";
  91.  
  92.                     if (matrix[santaRow, santaCol + 1] != "-")// right
  93.                     {
  94.                         presentsCount--;
  95.                         matrix[santaRow, santaCol + 1] = "-";
  96.                         if (presentsCount == 0)
  97.                         {
  98.                             break;
  99.                         }
  100.                     }
  101.  
  102.                     if (matrix[santaRow, santaCol - 1] != "-")// left
  103.                     {
  104.                         presentsCount--;
  105.                         matrix[santaRow, santaCol - 1] = "-";
  106.                         if (presentsCount == 0)
  107.                         {
  108.                             break;
  109.                         }
  110.                     }
  111.  
  112.                     if (matrix[santaRow - 1, santaCol] != "-")// up
  113.                     {
  114.                         presentsCount--;
  115.                         matrix[santaRow - 1, santaCol] = "-";
  116.                         if (presentsCount == 0)
  117.                         {
  118.                             break;
  119.                         }
  120.                     }
  121.  
  122.                     if (matrix[santaRow + 1, santaCol] == "V")// down
  123.                     {
  124.                         presentsCount--;
  125.                         matrix[santaRow + 1, santaCol] = "-";
  126.                         if (presentsCount == 0)
  127.                         {
  128.                             break;
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.  
  134.             int goodKidsLeft = 0;
  135.  
  136.             foreach (var item in matrix)
  137.             {
  138.                 if (item == "V")
  139.                 {
  140.                     goodKidsLeft++;
  141.                 }
  142.             }
  143.  
  144.             PrintMatrix(matrix);
  145.  
  146.             if (goodKidsLeft == 0)
  147.             {
  148.                 Console.WriteLine($"Good job, Santa! {goodKids} happy nice kid/s.");
  149.             }
  150.             else
  151.             {
  152.                 Console.WriteLine($"No presents for {goodKidsLeft} nice kid/s.");
  153.             }
  154.  
  155.         }
  156.  
  157.         static bool CheckBounderies(string[,] matrix, int santaRow, int santaCol)
  158.         {
  159.             return santaRow < 0 || santaRow >= matrix.GetLength(0) || santaCol < 0 || santaCol >= matrix.GetLength(1);
  160.         }
  161.  
  162.         private static void PrintMatrix(string[,] matrix)
  163.         {
  164.             for (int row = 0; row < matrix.GetLength(0); row++)
  165.             {
  166.                 for (int col = 0; col < matrix.GetLength(1); col++)
  167.                 {
  168.                     Console.Write(matrix[row, col] + " ");
  169.                 }
  170.                 Console.WriteLine();
  171.             }
  172.         }
  173.     }
  174. }
  175.  
Add Comment
Please, Sign In to add comment