#P10713. KDOI Robot Tournament
KDOI Robot Tournament
KDOI Robot Tournament
In the KDOI Robot Tournament, an infinite grid is used as the contest arena. Initially, the robot is located at $$(1,1)$$ and must reach $$(n,m)$$ (with $$n,m\ge 2$$). The robot moves by a sequence of operations. In the $$i$$-th operation, the contestant chooses one of the four directions (up, down, left, or right) and a positive integer $$x_i$$. Then the robot moves $$x_i$$ steps in that direction. However, due to a bug in the robot, the following restrictions apply:
- If $$i$$ is odd then $$x_i$$ must be odd.
- If $$i$$ is even then $$x_i$$ must be even.
The task is to compute the minimum number of operations required to move the robot from $$(1,1)$$ to $$(n,m)$$.
Note: You are allowed to choose both the magnitudes and the moving directions in each operation arbitrarily (subject to the parity restrictions). This means that if more than one operation is used in one direction, you may cancel out some extra distance. In other words, if a given operation does not exactly match the remaining distance, you can overshoot and then come back later as long as the parity conditions are met.
Analysis: Let $$dx=n-1$$ and $$dy=m-1$$ be the required displacements in the horizontal and vertical directions respectively. Notice that a single operation can only be used in one axis. Hence, at least one horizontal move and one vertical move must be scheduled. There are two possible assignments for the two operations:
- Assign an odd-indexed move (which must move an odd number of steps) to horizontal and an even-indexed move (which must move an even number of steps) to vertical. This assignment requires $$dx$$ to be odd and $$dy$$ to be even ($$dy\ge2$$ because an even-indexed move cannot move 1 step).
- Alternatively, assign an even-indexed move to horizontal and an odd-indexed move to vertical. This requires $$dx$$ to be even (with $$dx\ge2$$) and $$dy$$ to be odd.
If one of these assignments is possible then the answer is 2. Otherwise, in order to adjust the parity in one axis we need at least 3 operations.
Thus, the answer is determined as follows:
$$\text{If }\Big((dx\;\bmod\;2=1 \text{ and } dy\;\bmod\;2=0 \text{ and } dy\ge2) \quad \text{or} \quad (dx\;\bmod\;2=0 \text{ and } dx\ge2 \text{ and } dy\;\bmod\;2=1)\Big)\text{, then answer }=2; $$Otherwise, answer = 3.
For example:
- For $$n=2,m=2$$, we have $$dx=1$$ and $$dy=1$$. Neither assignment works, so the answer is 3.
- For $$n=2,m=3$$, we have $$dx=1$$ (odd) and $$dy=2$$ (even with $$dy\ge2$$), so the answer is 2.
- For $$n=3,m=2$$, we have $$dx=2$$ (even with $$dx\ge2$$) and $$dy=1$$ (odd), so the answer is 2.
- For $$n=3,m=3$$, we have $$dx=2$$ and $$dy=2$$; neither of the two assignments fits, thus answer = 3.
Your task is to implement this logic.
inputFormat
The input consists of a single line containing two integers $$n$$ and $$m$$ ($$n,m\ge2$$), which represent the target cell $$(n,m)$$.
outputFormat
Output a single integer: the minimum number of operations required to reach $$(n,m)$$ from $$(1,1)$$.
sample
2 2
3