Editorial for DMOPC '21 Contest 3 P3 - Hopping Frogs


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.

Author: 4fecta

For convenience, let's assume that all frogs from the first family are red and all frogs from the second family are blue.

Subtask 1

Just by trying a few cases out on paper, we observe that it is always possible to solve the problem by moving the red frog and the family of blue frogs in an alternating fashion.

Subtask 2

This subtask requires some additional experimentation. After solving some small cases where N=M, we notice that the frogs always arrange themselves in an alternating pattern of red, blue, red, blue, ... before they jump over each other and reach their destinations. It is thus possible to induct on this pattern in order to obtain a recursive solution, each time adding a single red and blue frog to the two ends of the pattern. Special care should be taken to note the colour of the first frog in the sequence in order to maintain the alternating pattern.

Subtask 3

First of all, let's calculate a closed form expression for the minimum number of hops required if a solution exists. We first note that for each pair of a red frog and a blue frog, one of the frogs has to jump over the other in order for their relative order to be switched. So, in total, there must be NM moves of the second type. Then, we note that each red frog is M+1 stones away from its target, and each blue frog is N+1 stones away from its target. Therefore, a total distance of N(M+1)+M(N+1) = 2NM + N + M needs to be traveled for all the frogs to reach their destinations. If we were to only make hops of the first type then we would need exactly 2NM + N + M hops, but each of the NM hops of the second type travels a distance of 2 instead of 1, each saving a single hop. Thus, the total number of moves required is 2NM + N + M - NM = NM + N + M. Note that this is also the only possible number of moves.

Now, let's try visualizing the problem by assigning a 2-D coordinate to each arrangement of frogs. Specifically, we map an arrangement of frogs to a point (x, y) if there are x red frogs to the right of the empty stone and y blue frogs to the left of the empty stone. Then, the initial arrangement of frogs corresponds to (0, 0), and the desired arrangement corresponds to (N, M). So, any valid solution should move our point from (0, 0) to (N, M). The diagram below shows a grid of all the possible points:

It is easy to see that there are (N+1)(M+1) = NM + N + M + 1 points in the grid. We may also note that through any sequence of moves, each point is traversed at most once. Given that we need to make exactly NM + N + M hops, it follows that any valid solution must visit every coordinate in the grid exactly once. Now, let's look at how each possible hop changes the coordinates of our point:

  • A red frog hops one stone to the right: the number of red frogs to the right of the empty stone increases by 1, so this hop moves (x, y) \rightarrow (x+1, y).
  • A blue frog hops one stone to the left: the number of blue frogs to the left of the empty stone increases by 1, so this hop moves (x, y) \rightarrow (x, y+1).
  • A red frog hops over a blue frog: the number of red frogs to the right of the empty stone increases by 1 and the number of blue frogs to the left of the empty stone decreases by 1, so this hop moves (x, y) \rightarrow (x+1, y-1).
  • A blue frog hops over a red frog: the number of blue frogs to the left of the empty stone increases by 1 and the number of red frogs to the right of the empty stone decreases by 1, so this hop moves (x, y) \rightarrow (x-1, y+1).

Visually, we can either move right, up, down-right, or up-left. It is not hard to see at this point that any path in this grid going from (0, 0) to (N, M) visiting every point exactly once corresponds to one of the following two patterns:

Now we can easily write a program that constructs one of these paths to solve the problem!


Comments

There are no comments at the moment.