Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _10.LadyBugs
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int fieldSize = int.Parse(Console.ReadLine());
  11.  
  12.             int[] field = new int[fieldSize];
  13.  
  14.             int[] bugsPositions = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)
  15.                 .Select(int.Parse)
  16.                 .ToArray();
  17.  
  18.             //place the bugs on the field
  19.             for (int i = 0; i < bugsPositions.Length; i++)
  20.             {
  21.                 if (bugsPositions[i] < fieldSize && bugsPositions[i] >= 0)
  22.                 {
  23.                     field[bugsPositions[i]] = 1;
  24.                 }
  25.                
  26.             }
  27.  
  28.             string input = Console.ReadLine();
  29.  
  30.             while (input != "end")
  31.             {
  32.                 string[] command = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  33.  
  34.                 int bugIndex = int.Parse(command[0]);
  35.  
  36.                 if (bugIndex >= 0 && bugIndex < fieldSize)
  37.                 {
  38.                     string direction = command[1];
  39.                     int step = int.Parse(command[2]);
  40.  
  41.                     if (field[bugIndex] == 1)
  42.                     {
  43.                         field[bugIndex] = 0;
  44.  
  45.                         if (direction == "right")
  46.                         {
  47.                             while (bugIndex + step < fieldSize && bugIndex + step >= 0)
  48.                             {
  49.                                 if (field[bugIndex + step] == 0)
  50.                                 {
  51.                                     field[bugIndex + step] = 1;
  52.                                     break;
  53.                                 }
  54.                                 else
  55.                                 {
  56.                                     bugIndex += step;
  57.                                 }
  58.                             }
  59.                         }
  60.                         else if (direction == "left")
  61.                         {
  62.                             while (bugIndex - step < fieldSize && bugIndex - step >= 0)
  63.                             {
  64.                                 if (field[bugIndex - step] == 0)
  65.                                 {
  66.                                     field[bugIndex - step] = 1;
  67.                                     break;
  68.                                 }
  69.                                 else
  70.                                 {
  71.                                     bugIndex -= step;
  72.                                 }
  73.                             }
  74.                         }
  75.                     }
  76.                 }
  77.                          
  78.                 input = Console.ReadLine();
  79.             }
  80.  
  81.             Console.WriteLine(string.Join(" ", field));
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement