knightPosition: {3,4}
targetPosition: {2,1}
The knight can move from position (3,4) to positions (1,3), (2,2) and (4,2). Position (4,2) is selected and the ‘stepCount’ becomes 1. From position (4,2), the knight can directly jump to the position (2,1) which is the target point and ‘stepCount’ becomes 2 which is the final answer.
1. The coordinates are 1 indexed. So, the bottom left square is (1,1) and the top right square is (N, N).
2. The knight can make 8 possible moves as given in figure 1.
3. A Knight moves 2 squares in one direction and 1 square in the perpendicular direction (or vice-versa).
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘3*T’ lines represent the ‘T’ test cases.
The first line of each test case contains a pair of integers denoting the initial position of the knight.
The second line of each input contains a pair of integers denoting the target position.
The third line of each test case contains an integer ‘N’ denoting the rows/columns of the chessboard.
For each test case, print an integer representing the minimum steps a Knight will take to reach the target position.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000
1 <= KNIGHTPOSITION(X, Y), TARGETPOSITION(X, Y) <= N
Time limit: 1 second
The idea is to perform BFS traversal. Start from the initial position of the Knight and proceed to further cells. Traversal is performed using a queue. The queue stores the different paths travelled by the knight and also keeps storing the count of steps. When the target is popped from the queue, the corresponding count of the steps is the answer.
Let us consider various cases:
1. If the position of the knight and the target cell are along different rows and columns, that is, ‘KNIGHT_X’ is not equal to ‘TARGET_X’ and ‘KNIGHT_Y’ is not equal to ‘TARGET_Y’.
Example:
(KNIGHT_X, KNIGHT_Y)={3,3} and (TARGET_X, TARGET_Y)={6,6}
There are 8 possible moves but only 2 steps move towards the target, which are {4,5} and {5,4}.
So, using dynamic programming, minSteps[(3,3) to (6,6)] = 1+ minSteps[(4,5) to (6,6)] or minSteps[(5,4) to (6,6)].
2. If the position of the knight and the target cell are along the same rows or columns, that is, ‘KNIGHT_X’ is equal to ‘TARGET_X’ or ‘KNIGHT_Y’ is equal to ‘TARGET_Y’.
Example:
(KNIGHT_X, KNIGHT_Y)={2,4} and (TARGET_X, TARGET_Y)={6,4}
There are in total 4 steps moving towards the target which are (4,3),(4,5),(3,6),(3,2).
However, (4,3) and (4,5) are equivalent as they are equally away from the target. Similarly, (3,6) and (3,2) are equivalent.
So, using dynamic programming, ‘MIN_STEPS’[(2,4) to (7,4)] = 1+ 'MIN_STEPS'[(4,5) to (6,4)] or 'MIN_STEPS'(3,6) to (6,4)].
3. If either the knight or the target is at the corner and [abs(KNIGHT_X - TARGET_X), abs(knightY-targetY)]=[1,1], minimum steps to reach the target will be 4.
Algorithm:
Stock Span
Stock Span
Hills and Soldier
Hills and Soldier
Hills and Soldier
Hills and Soldier
Hills and Soldier
Next Greater Element II
COUNT ISLANDS
Distance to a Cycle in Undirected Graph