Advertisement
Vladimir76

Ladybug

Feb 22nd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 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 Ladybugs
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             int sizeField = int.Parse(Console.ReadLine());
  14.             if (sizeField <= 0)
  15.             {
  16.                 return;
  17.             }
  18.             long[] indexOfLadybug = Console.ReadLine()
  19.                 .Trim()
  20.                 .Split()
  21.                 .Select(long.Parse)
  22.                 .ToArray();
  23.             long[] field = new long[sizeField];
  24.             for (int i = 0; i < indexOfLadybug.Length; i++)
  25.             {
  26.                 if (!(indexOfLadybug[i] < 0) && !(indexOfLadybug[i] >= field.Length))
  27.                 {
  28.                     field[indexOfLadybug[i]] = 1;
  29.                 }
  30.             }
  31.  
  32.             string command = Console.ReadLine();
  33.             if (String.IsNullOrEmpty(command))
  34.             {
  35.                 Console.WriteLine(string.Join(" ",field));
  36.                 return;
  37.             }
  38.             string[] commandStr = command
  39.                 .Trim()
  40.                 .Split();
  41.  
  42.             bool checkEmpty = true;
  43.             long endFly = 0;
  44.  
  45.             while (!commandStr[0].Equals("end"))
  46.             {
  47.                 long indexLady = long.Parse(commandStr[0]);
  48.                 string direction = commandStr[1];
  49.                 long lengthFly = long.Parse(commandStr[2]);
  50.                 switch (direction)
  51.                 {
  52.                     case "right":
  53.                         if (indexLady >= 0 && indexLady < field.Length && field[indexLady]==1)
  54.                         {
  55.                             endFly = indexLady + lengthFly;
  56.                             checkEmpty = true;
  57.                             if (lengthFly >= 0 && endFly < field.Length)
  58.                             {
  59.                                 do
  60.                                 {
  61.                                     if (field[endFly] != 1)
  62.                                     {
  63.                                         field[endFly] = 1;
  64.                                         field[indexLady] = 0;
  65.                                         checkEmpty = false;
  66.                                     }
  67.                                     else
  68.                                     {
  69.                                         endFly+=lengthFly;
  70.                                         if (endFly >= field.Length)
  71.                                         {
  72.                                             field[indexLady] = 0;
  73.                                             break;
  74.                                         }
  75.                                     }
  76.                                
  77.                                 } while (checkEmpty);
  78.                                 checkEmpty = true;
  79.                             }
  80.                             else if (lengthFly >= 0 && endFly >= field.Length)
  81.                             {
  82.                                 field[indexLady] = 0;
  83.                             }
  84.                             else if (lengthFly < 0 && endFly >= 0)
  85.                             {
  86.                                 do
  87.                                 {
  88.                                     if (field[endFly] != 1)
  89.                                     {
  90.                                         field[endFly] = 1;
  91.                                         field[indexLady] = 0;
  92.                                         checkEmpty = false;
  93.                                     }
  94.                                     else
  95.                                     {
  96.  
  97.                                         endFly-=lengthFly;
  98.                                         if (endFly < 0)
  99.                                         {
  100.                                             field[indexLady] = 0;
  101.                                             break;
  102.                                         }
  103.                                     }
  104.                                 } while (checkEmpty);
  105.                                 checkEmpty = true;
  106.                             }
  107.                             else if (lengthFly < 0 && endFly < 0 )
  108.                             {
  109.                                 field[indexLady] = 0;
  110.                             }
  111.                         }
  112.                         break;
  113.                     case "left":
  114.                         if (indexLady >= 0 && indexLady < field.Length && field[indexLady]==1)
  115.                         {
  116.                             endFly = indexLady - lengthFly;
  117.  
  118.                             if (lengthFly >= 0 && endFly >= 0)
  119.                             {
  120.                                 checkEmpty = true;
  121.                                 do
  122.                                 {
  123.                                     if (field[endFly] != 1)
  124.                                     {
  125.                                         field[endFly] = 1;
  126.                                         field[indexLady] = 0;
  127.                                         checkEmpty = false;
  128.                                     }
  129.                                     else
  130.                                     {
  131.                                         endFly-=lengthFly;
  132.                                         if (endFly < 0)
  133.                                         {
  134.                                             field[indexLady] = 0;
  135.                                             break;
  136.                                         }
  137.                                     }
  138.                                 } while (checkEmpty);
  139.                                 checkEmpty = true;
  140.                             }
  141.                             else if (lengthFly >= 0 && endFly < 0)
  142.                             {
  143.                                 field[indexLady] = 0;
  144.                             }
  145.                             else if (lengthFly < 0 && endFly < field.Length)
  146.                             {
  147.                                 checkEmpty = true;
  148.                                 do
  149.                                 {
  150.                                     if (field[endFly] != 1)
  151.                                     {
  152.                                         field[endFly] = 1;
  153.                                         field[indexLady] = 0;
  154.                                         checkEmpty = false;
  155.                                     }
  156.                                     else
  157.                                     {
  158.                                         endFly+=lengthFly;
  159.                                         if (endFly >= field.Length)
  160.                                         {
  161.                                             field[indexLady] = 0;
  162.                                             break;
  163.                                         }
  164.                                     }
  165.                                 } while (checkEmpty);
  166.                                 checkEmpty = true;
  167.                             }
  168.                             else if (lengthFly < 0 && endFly >= field.Length)
  169.                             {
  170.                                 field[indexLady] = 0;
  171.                             }
  172.                         }
  173.                         break;
  174.                     default:
  175.                         break;
  176.                 }
  177.                 commandStr = Console.ReadLine()
  178.                 .Trim()
  179.                 .Split();
  180.             }
  181.             Console.WriteLine(string.Join(" ",field));
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement