Advertisement
valkata

LadyBugs

Aug 15th, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _02_ladyBugs
  8. {
  9.     class ladyBugs
  10.     {
  11.         static long[] field;
  12.         static void Main(string[] args)
  13.         {
  14.             field = new long[long.Parse(Console.ReadLine())];
  15.             long[] ladybugpossition = Console.ReadLine().Split().Select(long.Parse).ToArray();
  16.             field = BugsInField(field, ladybugpossition);
  17.  
  18.             string input = Console.ReadLine();
  19.  
  20.             while (input != "end")
  21.             {
  22.                 string[] inputTokkens = input.Split().ToArray();
  23.                 long ladybug = long.Parse(inputTokkens[0]);
  24.                 string direction = inputTokkens[1];
  25.                 long flyLength = long.Parse(inputTokkens[2]);
  26.  
  27.                 switch (direction)
  28.                 {
  29.                     case "right":
  30.                         {
  31.                             MoveRight(ladybug, flyLength);
  32.                             break;
  33.                         }
  34.                     case "left":
  35.                         {
  36.                             MoveLeft(ladybug, flyLength);
  37.                             break;
  38.                         }
  39.                 }
  40.                 input = Console.ReadLine();
  41.             }
  42.  
  43.             Console.WriteLine(string.Join(" ", field));
  44.         }
  45.  
  46.         private static void MoveLeft(long ladybug, long flyLength)
  47.         {
  48.             if (ladybug >= 0 && ladybug < field.Length)
  49.             {
  50.                 if (field[ladybug] == 1)
  51.                 {
  52.                     long startIndex = ladybug;
  53.                     field[ladybug] = 0;
  54.                     if (flyLength > 0)
  55.                     {
  56.                         for (long i = startIndex - flyLength; i >= 0; i--)
  57.                         {
  58.                             if (field[i] == 0)
  59.                             {
  60.                                 field[i] = 1;
  61.                                 return;
  62.                             }
  63.                         }
  64.                     }
  65.                     else
  66.                     {
  67.                         for (long i = startIndex - flyLength; i < field.Length; i++)
  68.                         {
  69.                             if (field[i] == 0)
  70.                             {
  71.                                 field[i] = 1;
  72.                                 return;
  73.                             }
  74.                         }
  75.                     }
  76.                 }
  77.                 else
  78.                 {
  79.                     return;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         private static void MoveRight(long ladybug, long flyLength)
  85.         {
  86.             if (ladybug >= 0 && ladybug < field.Length)
  87.             {
  88.                 if (field[ladybug] == 1)
  89.                 {
  90.                     long startIndex = ladybug;
  91.                     field[ladybug] = 0;
  92.                     if (flyLength > 0)
  93.                     {
  94.                         for (long i = startIndex + flyLength; i < field.Length; i++)
  95.                         {
  96.                             if (field[i] == 0)
  97.                             {
  98.                                 field[i] = 1;
  99.                                 return;
  100.                             }
  101.                         }
  102.                     }
  103.                     else
  104.                     {
  105.                         for (long i = startIndex + flyLength; i >= 0; i--)
  106.                         {
  107.                             if (field[i] == 0)
  108.                             {
  109.                                 field[i] = 1;
  110.                                 return;
  111.                             }
  112.                         }
  113.                     }
  114.                 }
  115.                 else
  116.                 {
  117.                     return;
  118.                 }
  119.             }
  120.         }
  121.  
  122.         private static long[] BugsInField(long[] field, long[] ladybugpossition)
  123.         {
  124.             for (int i = 0; i < field.Length; i++)
  125.             {
  126.                 for (int j = 0; j < ladybugpossition.Length; j++)
  127.                 {
  128.                     if (i == ladybugpossition[j] && ladybugpossition[j] < field.Length && ladybugpossition[j] >= 0)
  129.                     {
  130.                         field[i] = 1;
  131.                         break;
  132.                     }
  133.                 }
  134.             }
  135.             return field;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement