10.    *Crossroads 
arnold avatar arnold 29 Точки

10. *Crossroads (C# Stacks and Queues)

Отново се налага да прибягна до жокер - помощ от публиката 71/100. Пак се затрудних със задача от стекове и опашки.






10.    *Crossroads 

Our favorite super-spy action hero Sam is back from his mission in the previous exam, and he has finally found some time to go on a holiday. He is taking his wife somewhere nice and they’re going to have a really good time, but first, they have to get there. Even on his holiday trip, Sam is still going to run into some problems and the first one is, of course, getting to the airport. Right now, he is stuck in a traffic jam at a very active crossroads where a lot of accidents happen.

Your job is to keep track of traffic at the crossroads and report whether a crash happened or everyone passed the crossroads safely and our hero is one step closer to a much desired vacation.

The road Sam is on has a single lane where cars queue up until the light goes green. When it does, they start passing one by one during the green light and the free window before the intersecting road’s light goes green. During one second only one part of a car (a single character) passes the crossroads. If a car is still in the crossroads when the free window ends, it will get hit at the first character that is still in the crossroads.


  • On the first line, you will receive the duration of the green light in seconds – an integer in the range [1-100]
  • On the second line, you will receive the duration of the free window in seconds – an integer in the range [0-100]
  • On the following lines, until you receive the "END" command, you will receive one of two things:
    • A car – a string containing any ASCII character, or
    • The command "green" which indicates the start of a green light cycle

A green light cycle goes as follows:

  • During the green light cars will enter and exit the crossroads one by one
  • During the free window cars will only exit the crossroads


  • If a crash happens, end the program and print:
    "A crash happened!"
    "{car} was hit at {characterHit}."
  • If everything goes smoothly and you receive an "END" command, print:
    "Everyone is safe."
    "{totalCarsPassed} total cars passed the crossroads."


  • The input will be within the constaints specified above and will always be valid. There is no need to check it explicitly.














Everyone is safe.

3 total cars passed the crossroads.

During the first green light (10 seconds), the Mercedes (8) passes safely.

During the second green light, the Mercedes (8) passes safely and there are 2 seconds left.

The BMW enters the crossroads and when the green light ends, it still has 1 part inside ('W'), but has 5 seconds to leave and passes successfully.

The Skoda never enters the crossroads, so 3 cars passed successfully.










A crash happened!

Hummer was hit at e.

Mercedes (8) passes successfully and Hummer (6) enters the crossroads but only the 'H' passes during the green light. There are 3 seconds of free window, so "umm" passes and the Hummer gets hit at 'e' and the program ends with a crash.



Module: C# Advanced 11/09/2021 15:47:27
svephoto avatar svephoto 1019 Точки

Здравей, arnold,


Виж какво ще стане, ако махнеш равното в проверката на ред 35, т. е. ако е


if (cars.Peek().Length < currentTime)


П. С. Всъщност, сега виждам в Judge, че вече си стигнал до вярното решение за 100/100, браво. yes laugh

11/09/2021 21:46:55
arnold avatar arnold 29 Точки

Да, просто добавих и за зелената светлина докато е по-голяма от нула да върти цикъла :)

while (cars.Count > 0 && currentTime > 0), вместо само while (cars.Count > 0). От там ми е гърмяло, защото ако има чакаше кола в опашката, а зелената светлина е станала 0 след предната кола, ще влезе и ще "крашне" и тя, а не трябва.

12/09/2021 13:52:42