C# – CodeForces – Sagheer and Crossroads

Codeforces.com is the web site, that lets you check your algorithmic skills, in case that you want to see where you are, compared to the world. Thus, today I was checking the problems in the 2. Division and the first problem seemed interesting. E.g., with other words – doable in less than 15 minutes.

This is what the problem was looking like:

Sagheer is walking in the street when he comes to an intersection of two roads. Each road can be represented as two parts where each part has 3 lanes getting into the intersection (one for each direction) and 3 lanes getting out of the intersection, so we have 4 parts in total. Each part has 4 lights, one for each lane getting into the intersection (l — left, s — straight, r — right) and a light p for a pedestrian crossing.

An accident is possible if a car can hit a pedestrian. This can happen if the light of a pedestrian crossing of some part and the light of a lane that can get to or from that same part are green at the same time.

Now, Sagheer is monitoring the configuration of the traffic lights. Your task is to help him detect whether an accident is possible.


The trick in the problem is the following – there are only 6 lanes of the crossroad, that may cause an accident with a pedestrian – each of the three lanes, which are are in the pedestrian’s road + one lane per road, going into that road. Having said that, we simply have to check if one of this conditions is true and repeat the check four times. We put a condition before the condition, just to make sure that we check only cases, where we have a pedestrians’ traffic light green.

Something like this:

That’s all! 🙂


Tagged with: ,