Advertisement
MARINA_GREBENAROVA

10.LadyBugs

Jan 23rd, 2022
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 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(string[] args)
  9.         {
  10.             int fieldSize = int.Parse(Console.ReadLine());
  11.  
  12.             //bool[] field = new bool[fieldSize];
  13.  
  14.             int[] initialIndexes = Console.ReadLine()
  15.                 .Split()
  16.                 .Select(int.Parse)
  17.                 .ToArray();
  18.  
  19.             int[] field = new int[fieldSize];
  20.  
  21.             for (int i = 0; i < initialIndexes.Length; i++)
  22.             {
  23.                 int currentIndex = initialIndexes[i];
  24.                 if (currentIndex >= 0 && currentIndex < field.Length)
  25.                 {
  26.  
  27.                     field[currentIndex] = 1;
  28.                 }
  29.  
  30.  
  31.             }
  32.             string command = String.Empty;
  33.             while ((command = Console.ReadLine()) != "end")
  34.             {
  35.                 string[] elements = command.Split();
  36.                 int ladyBugIndex = int.Parse(elements[0]);
  37.                 string direction = elements[1];
  38.                 int flyLength = int.Parse(elements[2]);
  39.  
  40.                 if (ladyBugIndex < 0 || ladyBugIndex > field.Length - 1 || field[ladyBugIndex] == 0)
  41.                 {
  42.                     continue;
  43.                 }
  44.                 field[ladyBugIndex] = 0;
  45.  
  46.                 if (direction == "right")
  47.                 {
  48.                     int landIndex = ladyBugIndex + flyLength;
  49.                     if (landIndex > field.Length - 1)
  50.                     {
  51.                         continue;
  52.                     }
  53.                     if (field[landIndex] ==1)
  54.                     {
  55.                         while (field[landIndex] ==1)
  56.                         {
  57.                             landIndex += flyLength;
  58.                             if (landIndex > field.Length -1)
  59.                             {
  60.                                 break;
  61.                             }
  62.                         }
  63.                     }
  64.                     if (landIndex >=0 && landIndex <= field.Length - 1)
  65.                     {
  66.                         field[landIndex] = 1;
  67.                     }
  68.                    
  69.                 }
  70.                 else if (direction == "left")
  71.                 {
  72.                     int landIndex = ladyBugIndex - flyLength;
  73.                     if (landIndex < 0)
  74.                     {
  75.                         continue;
  76.                     }
  77.                     if (field[landIndex] == 1)
  78.                     {
  79.                         while (field[landIndex] == 1)
  80.                         {
  81.                             landIndex -= flyLength;
  82.                             if (landIndex < 0)
  83.                             {
  84.                                 break;
  85.                             }
  86.                         }
  87.                     }
  88.                     if (landIndex >= 0 && landIndex <= field.Length - 1)
  89.                     {
  90.                         field[landIndex] = 1;
  91.                     }
  92.                 }
  93.             }
  94.             Console.WriteLine(String.Join(' ', field));
  95.  
  96.  
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement