#P5003. Turn Counting on a Grid
Turn Counting on a Grid
Turn Counting on a Grid
You are given a grid where the starting point is at \((1,1)\) (top-left corner) and the destination is at \((M,N)\). You can only move to the right or down (i.e. only along integer grid intersections).
A turn is defined as a change in your moving direction (from right to down or from down to right). Note that if you move continuously in one direction, it does not count as a turn.
Your task is to calculate the minimum and maximum number of turns among all valid paths from the start to the destination.
Clarifications:
- If either \(M=1\) or \(N=1\), you are moving in a straight line, and both the minimum and maximum number of turns will be \(0\).
- For a grid where both \(M>1\) and \(N>1\):
- The minimum number of turns is \(1\) (e.g. moving all the way in one direction and then turning and continuing in the other).
- The maximum number of turns is achieved by alternating directions as often as possible. Let the number of right moves be \(R = N-1\) and down moves be \(D = M-1\). Then the maximum number of turns is computed as follows:
- If \(R = D\), then maximum turns = \(2R - 1\).
- If \(R \neq D\), then maximum turns = \(2\min(R,D)\).
Input Format: Two space-separated integers \(M\) and \(N\) representing the dimensions of the grid.
Output Format: Two integers separated by a space: the minimum number of turns and the maximum number of turns.
inputFormat
The input consists of one line containing two integers \(M\) and \(N\) (separated by a space), where \(M,N \ge 1\).
outputFormat
Output a single line with two integers separated by a space: the minimum and maximum number of turns required to reach from \((1,1)\) to \((M,N)\).
sample
2 2
1 1