New update is available. Click here to update.

Last Updated: 21 Nov, 2020

Difficulty: Moderate

```
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.

- Define a class with the following data members

a. ‘X_COORDINATE’ denoting the x-coordinate

b. ‘Y_COORDINNATE’ denoting the y-coordinate

c. ‘STEP_COUNT’ denoting the number of steps. - Take two arrays ‘DIRECTION_X’ and ‘DIRECTION_Y’ storing the direction in which the knight can move.
- Create a queue to store the class objects.
- Take a visited array to mark the cells already visited and initialize it to false.
- Push the initial position of the knight to the queue and mark it as visited.
- Loop till the queue is not empty.
- Pop the front element.

a. If the current cell is equal to the target cell, return the 'STEP_COUNT’ which is the minimum step a Knight will take to reach the target position.

b. Else, loop for all the reachable coordinates. If the coordinate is not yet visited and is reachable, push it to the queue along with the incremented value of ‘STEP_COUNT’.

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:

- Initialize a ‘DP’ array to store the subpaths to -1.
- We’ll have the following base cases:

DP[a][b] where ‘a’ and ‘b’ is the difference between ‘KNIGHT_X’ and ‘TARGET_X’ and ‘KNIGHT_Y’ and ‘KNIGHT_Y’ respectively.

DP[1][0] = 3, DP[0][1] = 3, DP[1][1] = 2, DP[2][0] = 2, DP[0][2] =2, DP[2][1] =1, DP[1][2] =1. - Add all base cases which satisfies the case 3 given above.
- If the target is reached by the knight, return 0.
- If the cell is revisited, return the stored answer in the ‘DP’ array.
- Else, select the coordinates which satisfy cases 1 and 2 given above.
- Store 1 plus a minimum of the paths obtained by two coordinates in the ‘DP’ array and recur till the target is not reached.
- Exchanging the coordinates x with y of both knight and target will result in the same answer. So, we’ll update the ‘DP’ array accordingly.
- Return DP[ abs(KNIGHT_X - TARGET_X)] [ abs(KNIGNT_Y - TARGET_Y)] as the minimum steps a Knight will take to reach the target position.

SIMILAR PROBLEMS

Stock Span

Posted: 16 Jun, 2022

Difficulty: Moderate

Hills and Soldier

Posted: 7 Jul, 2022

Difficulty: Moderate

Hills and Soldier

Posted: 7 Jul, 2022

Difficulty: Moderate

Next Greater Element II

Posted: 9 Sep, 2022

Difficulty: Moderate

COUNT ISLANDS

Posted: 14 Sep, 2022

Difficulty: Moderate

Distance to a Cycle in Undirected Graph

Posted: 7 Nov, 2022

Difficulty: Moderate

Popular Interview Problems: