Editorial for COCI '23 Contest 4 #5 Roboti


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

For k = 0, just answer 0 if x_1 = x_2 or y_1 = y_2 and -1 otherwise.

For n, m \le 300, q \le 10, for each query we can simulate a path in each direction from (x_1, y_1) and see if it reaches (x_2, y_2) and count turns along the way. It might end up in an infinite loop; we can force it to stop after 10^6 moves, because it's impossible for the distance to (x_2, y_2) to be that high.

For n, m \le 300, we can imagine a graph where each pair of (x, y) and a direction is considered a vertex, and there is an edge from one vertex to another if, after going from (x, y) in the direction given by the initial vertex, our next move will be going from (x, y) of the next vertex in the direction given by the next vertex. Notice that each vertex has outdegree 1 and outdegree 1, so we have a graph which is a union of cycles. Now we can easily see if 2 vertices are reachable from one another by checking if they're in the same cycle, and also find the number of turns between them. We can do that for all 4 vertices that go out of (x_1, y_1) and all 4 vertices that go into (x_2, y_2) and see which pair gives the smallest number of turns.

For the full solution, notice that, outside of the case where x_1 = x_2 and row x_1 doesn't have anything in it or y_1 = y_2 and column y_1 doesn't have anything in it, the only (x, y) that you need to make vertices for are the k given (x, y). With that, finding the minimum number of turns becomes quite easy, as every vertex in the cycle represents one turn.


Comments

There are no comments at the moment.