Given a square chessboard of N x N size, the position of Knight and position of a target is given. We need to find out the minimum steps a Knight will take to reach the target position.
In above diagram Knight takes 3 step to reach from (4, 5) to (1, 1) (4, 5) -> (5, 3) -> (3, 2) -> (1, 1) as shown in diagram
This problem can be seen as shortest path in unweighted graph. Therefore we use BFS to solve this problem. We try all 8 possible positions where a Knight can reach from its position. If reachable position is not already visited and is inside the board, we push this state into queue with distance 1 more than its parent state. Finally we return distance of target position, when it gets pop out from queue.
Below code implements BFS for searching through cells, where each cell contains its coordinate and distance from starting node. In worst case, below code visits all cells of board, making worst-case time complexity as O(N^2)
- Time complexity: O(N^2).
At worst case, all the cells will be visited so time complexity is O(N^2).
- Auxiliary Space: O(N^2).
The nodes are stored in queue. So the space Complexity is O(N^2).
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum steps to reach target by a Knight | Set 2
- Minimum steps to reach a destination
- Minimum steps to reach end of array under constraints
- Minimum steps required to reach the end of a matrix | Set 2
- Minimum steps to reach any of the boundary edges of a matrix | Set 1
- Minimum steps to reach any of the boundary edges of a matrix | Set-2
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Puzzle | Can a Knight reach bottom from top by visiting all squares
- Minimize the number of steps required to reach the end of the array | Set 2
- Word Ladder (Length of shortest chain to reach a target word)
- Minimum step to reach one
- Minimum steps to color the tree with given colors
- Minimum number of jumps to reach end
- Minimum possible modifications in the matrix to reach destination
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
- Find the minimum number of moves to reach end of the array
- Minimum total cost incurred to reach the last station
- Find the minimum cost to reach destination using a train
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix