Given a square chessboard of N x N size, the position of Knight and position of a target is given, the task is to find out the minimum steps a Knight will take to reach the target position.
Input : (2, 4) - knight's position, (6, 4) - target cell Output : 2 Input : (4, 5) (1, 1) Output : 3
A BFS approach to solve the above problem has already been discussed in the previous post. In this a post, a Dynamic Programming solution is discussed.
Explanation of the approach :
- Case 1 : If target is not along one row or one column of knight’s position.
Let a chess board of 8 x 8 cell. Now, let say knight is at (3, 3) and the target is at (7, 8). There are possible 8 moves from the current position of knight i.e. (2, 1), (1, 2), (4, 1), (1, 4), (5, 2), (2, 5), (5, 4), (4, 5). But, among these only two moves (5, 4) and (4, 5) will be towards the target and all other goes away from the target. So, for finding minimum steps go to either (4, 5) or (5, 4). Now, calculate the minimum steps taken from (4, 5) and (5, 4) to reach the target. This is calculated by dynamic programming. Thus, this results in the minimum steps from (3, 3) to (7, 8).
- Case 2 : If the target is along one row or one column of knight’s position.
Let a chess board of 8 x 8 cell. Now, let’s say knight is at (4, 3) and the target is at (4, 7). There are possible 8 moves but towards the target, there are only 4 moves i.e. (5, 5), (3, 5), (2, 4), (6, 4). As, (5, 5) is equivalent to (3, 5) and (2, 4) is equivalent to (6, 4). So, from these 4 points, it can be converted into 2 points. Taking (5, 5) and (6, 4) (here). Now, calculate the minimum steps taken from these two points to reach the target. This is calculated by dynamic programming. Thus, this results in the minimum steps from (4, 3) to (4, 7).
Exception : When the knight will be at corner and the target is such that the difference of x and y coordinates with knight’s position is (1, 1) or vice-versa. Then minimum steps will be 4.
Dynamic Programming Equation :
1) dp[diffOfX][diffOfY] is the minimum steps taken from knight’s position to target’s position.
2) dp[diffOfX][diffOfY] = dp[diffOfY][diffOfX].
where, diffOfX = difference between knight’s x-coordinate and target’s x-coordinate
diffOfY = difference between knight’s y-coordinate and target’s y-coordinate
Below is the implementation of above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum steps to reach target by a Knight | Set 1
- Count of all possible ways to reach a target by a Knight
- Puzzle | Can a Knight reach bottom from top by visiting all squares
- Find minimum steps required to reach the end of a matrix | Set - 1
- Minimum steps to reach any of the boundary edges of a matrix | Set 1
- Find minimum steps required to reach the end of a matrix | Set 2
- Find minimum number of steps to reach the end of String
- Minimum steps to reach a destination
- The Knight's tour problem | Backtracking-1
- Probability of Knight to remain in the chessboard
- Warnsdorff's algorithm for Knight’s tour problem
- Possible moves of knight
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Count all possible position that can be reached by Modified Knight
- Minimize the number of steps required to reach the end of the array
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
- Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
Improved By : princiraj1992