Advertisement
vinikol

Problem 2. Ladybugs

Oct 25th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 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 void Main(string[] args)
  12.         {
  13.             var sizeField = long.Parse(Console.ReadLine());
  14.             var initialIndexes = Console.ReadLine()
  15.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  16.                 .Select(long.Parse)
  17.                 .ToList();
  18.             var command = Console.ReadLine()
  19.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  20.                 .ToArray();
  21.  
  22.             bool[] occupiedIndexes = new bool[sizeField];
  23.             for (long i = 0; i < sizeField; i++)
  24.             {
  25.                 if (initialIndexes.Contains(i))
  26.                 {
  27.                     occupiedIndexes[i] = true;
  28.                 }
  29.             }
  30.  
  31.             while (command[0] != "end")
  32.             {
  33.                 if (command[1] == "right")
  34.                 {
  35.                     var positionToTake = long.Parse(command[0]);
  36.                     var positionsToMove = long.Parse(command[2]);
  37.  
  38.                     if (occupiedIndexes[positionToTake] == false)
  39.                     {
  40.                         command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  41.                         continue;
  42.                     }
  43.                     else
  44.                     {
  45.                         occupiedIndexes[positionToTake] = false;
  46.                         if (positionToTake + positionsToMove >= sizeField)
  47.                         {
  48.                             command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  49.                             continue;
  50.                         }
  51.  
  52.                         if (occupiedIndexes[positionToTake + positionsToMove] == false)
  53.                         {
  54.                             occupiedIndexes[positionToTake + positionsToMove] = true;
  55.                         }
  56.                         else
  57.                         {
  58.                             for (long i = positionToTake + positionsToMove; i < sizeField; i++)
  59.                             {
  60.                                 if (occupiedIndexes[i] == false)
  61.                                 {
  62.                                     occupiedIndexes[i] = true;
  63.                                     break;
  64.                                 }
  65.                             }
  66.                         }
  67.                     }
  68.                 }
  69.                 else
  70.                 {
  71.                     var positionToTake = long.Parse(command[0]);
  72.                     var positionsToMove = long.Parse(command[2]);
  73.  
  74.                     if (occupiedIndexes[positionToTake] == false)
  75.                     {
  76.                         command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  77.                         continue;
  78.                     }
  79.                     else
  80.                     {
  81.  
  82.                         occupiedIndexes[positionToTake] = false;
  83.                         if (positionToTake - positionsToMove < 0)
  84.                         {
  85.                             command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  86.                             continue;
  87.                         }
  88.  
  89.                         if (occupiedIndexes[positionToTake - positionsToMove] == false)
  90.                         {
  91.                             occupiedIndexes[positionToTake - positionsToMove] = true;
  92.                         }
  93.                         else
  94.                         {
  95.                             for (long i = positionToTake - positionsToMove; i >= 0; i--)
  96.                             {
  97.                                 if (occupiedIndexes[i] == false)
  98.                                 {
  99.                                     occupiedIndexes[i] = true;
  100.                                     break;
  101.                                 }
  102.                             }
  103.                         }
  104.                     }
  105.                 }
  106.                 command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  107.             }
  108.  
  109.             foreach (var position in occupiedIndexes)
  110.             {
  111.                 if (position == false)
  112.                 {
  113.                     Console.Write("0 ");
  114.                 }
  115.                 else
  116.                 {
  117.                     Console.Write("1 ");
  118.                 }
  119.             }
  120.             Console.WriteLine();
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement