Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Numerics;
- using System.Text;
- namespace _02.Ladybugs
- {
- class Program
- {
- static void Main(string[] args)
- {
- var sizeOfTheFiled = int.Parse(Console.ReadLine());
- var field = new int[sizeOfTheFiled];
- var ladybugsIndexes = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .Where(i => i >= 0 && i < sizeOfTheFiled) // Only these between 0 and sizeOfTheFiled !
- .ToList();
- // fill the arr(filed) -> (If sizeOfTheFiled = 3 and ladybugsIndexes = 0 1 -> field[0] = 1 and filed[1] = 1; -> ! Debug !
- foreach (var index in ladybugsIndexes)
- {
- field[index] = 1;
- }
- while (true)
- {
- var command = Console.ReadLine().Split(' ');
- if (command[0] == "end")
- {
- break;
- }
- var currentLadybugIndex = int.Parse(command[0]);
- var direction = command[1];
- var flyLength = int.Parse(command[2]);
- // Ако е "left" обръщаме знака !
- if (direction == "left")
- {
- flyLength *= -1;
- }
- // outside of the field !
- if (currentLadybugIndex < 0 || currentLadybugIndex >= sizeOfTheFiled)
- {
- continue;
- }
- //there is no ladybug in this cell !
- if (field[currentLadybugIndex] == 0)
- {
- continue;
- }
- // Fly away
- field[currentLadybugIndex] = 0;
- // Set the nextIndex to land !
- var nextIndexToLand = currentLadybugIndex;
- while (true)
- { // increase to the next indexToLand , if there is ladybug in the cell !
- nextIndexToLand += flyLength;
- // outside of the field !
- if (nextIndexToLand < 0 || nextIndexToLand >= sizeOfTheFiled)
- {
- break;
- }
- // there is ladybug in this cell !
- if (field[nextIndexToLand] == 1)
- {
- continue;
- }
- // If the current cell is empty(field[nextIndexToLand] == 0 ,set it to 1), land here and break !
- field[nextIndexToLand] = 1;
- break;
- }
- }
- Console.WriteLine(string.Join(" ", field));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement