Advertisement
Danny_Berova

02.Ladybugs

Aug 11th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1.  
  2. namespace _02.Ladybugs
  3. {
  4.     using System;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.  
  8.     public class Program
  9.     {
  10.         public static void Main()
  11.         {
  12.             int fieldSize = int.Parse(Console.ReadLine());
  13.  
  14.             int[] field = new int[fieldSize];
  15.  
  16.             int[] bugsIndices = Console.ReadLine().Split().Select(int.Parse).ToArray();
  17.  
  18.             foreach (var bugIndeks in bugsIndices)
  19.             {
  20.                 if (bugIndeks < 0 || bugIndeks >= field.Length)
  21.                 {
  22.                     continue;
  23.                 }
  24.  
  25.                 field[bugIndeks] = 1;
  26.             }
  27.  
  28.             while (true)
  29.             {
  30.                 string commands = Console.ReadLine();
  31.                 if (commands == "end")
  32.                 {
  33.                     break;
  34.                 }
  35.                
  36.                string[] tokens = commands.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  37.  
  38.                 int bugIndex = int.Parse(tokens[0]);
  39.                 string direction = tokens[1];
  40.                 int flyLenght = int.Parse(tokens[2]);
  41.  
  42.                 if (bugIndex < 0 || bugIndex >= fieldSize)
  43.                 {
  44.                     continue;
  45.                 }
  46.  
  47.                 if (field[bugIndex] == 0)
  48.                 {
  49.                     continue;
  50.                 }
  51.  
  52.                 field[bugIndex] = 0;
  53.                 int position = bugIndex;
  54.  
  55.                 while (true)
  56.                 {
  57.                     if (direction == "right")
  58.                     {
  59.                         position += flyLenght;
  60.                     }
  61.                     else
  62.                     {
  63.                         position -= flyLenght;
  64.                     }
  65.  
  66.                     if (position < 0 || position >= fieldSize)
  67.                     {
  68.                         break;
  69.                     }
  70.  
  71.                     if (field[position] == 1)
  72.                     {
  73.                         continue;
  74.                     }
  75.                     else
  76.                     {
  77.                         field[position] = 1;
  78.                         break;
  79.                     }
  80.                 }
  81.             }
  82.  
  83.             Console.WriteLine(string.Join(" ", field));
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement