Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- class Program
- {
- static void Main(string[] args)
- {
- int fieldSize = int.Parse(Console.ReadLine()); //размера на полето
- int[] field = new int[fieldSize]; //самото поле
- int[] ladybugIndexes = Console.ReadLine() //индексите на които има калинки
- .Split(' ')
- .Select(int.Parse)
- .Where(x => x >= 0 && x < fieldSize) //премахваме индексите които са извън размера на полето
- .ToArray();
- foreach (var bugIndex in ladybugIndexes) //обхождаме индексите където ще има калинки
- {
- field[bugIndex] = 1; //слагаме калинка
- }
- string commandInput = string.Empty;
- while ((commandInput = Console.ReadLine()) != "end")
- {
- string[] command = commandInput.Split(' ');
- int currentIndex = int.Parse(command[0]); //индекса на който се намираме
- string direction = command[1]; //посоката на летене
- int flyLength = int.Parse(command[2]);
- flyLength = direction == "right" ? flyLength : -flyLength; //ако летим надясно + ако летим на ляво -
- if (currentIndex < 0 || currentIndex >= fieldSize || field[currentIndex] == 0)
- {
- continue; //ако настоящия индекс е извън рамките на масива или няма калинка на него скипваме
- }
- field[currentIndex] = 0; //калинката е отлетяла слагаме 0 на нейно място
- currentIndex += flyLength; //първия и полет
- while (currentIndex >= 0 && currentIndex < fieldSize) //проверяваме дали сме в рамките на полето
- {
- if (field[currentIndex] == 0) //ако намерим парко място
- {
- field[currentIndex] = 1; //паркираме и прекъсваме
- break;
- }
- currentIndex += flyLength; //ако не сме намерили парко продължаваме да летим
- }
- }
- Console.WriteLine(string.Join(" ", field)); //принтираме
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement