Given an NxN chessboard and a Knight at position (x,y). The Knight has to take exactly K steps, where at each step it chooses any of the 8 directions uniformly at random. What is the probability that the Knight remains in the chessboard after taking K steps, with the condition that it can’t enter the board again once it leaves it?
Let's take: 8x8 chessboard, initial position of the knight : (0, 0), number of steps : 1 At each step, the Knight has 8 different positions to choose from. If it starts from (0, 0), after taking one step it will lie inside the board only at 2 out of 8 positions, and will lie outside at other positions. So, the probability is 2/8 = 0.25
One thing that we can observe is that at every step the Knight has 8 choices to choose from. Suppose, the Knight has to take k steps and after taking the Kth step the knight reaches (x,y). There are 8 different positions from where the Knight can reach to (x,y) in one step, and they are: (x+1,y+2), (x+2,y+1), (x+2,y-1), (x+1,y-2), (x-1,y-2), (x-2,y-1), (x-2,y+1), (x-1,y+2).
What if we already knew the probabilities of reaching these 8 positions after K-1 steps?
Then, the final probability after K steps will simply be equal to the (Σ probability of reaching each of these 8 positions after K-1 steps)/8;
Here we are dividing by 8 because each of these 8 positions has 8 choices and position (x,y) is one of the choices.
For the positions that lie outside the board, we will either take their probabilities as 0 or simply neglect it.
Since we need to keep track of the probabilities at each position for every number of steps, we need Dynamic Programming to solve this problem.
We are going to take an array dp[x][y][steps] which will store the probability of reaching (x,y) after (steps) number of moves.
Base case: if the number of steps is 0, then the probability that the Knight will remain inside the board is 1.
Below is the implementation of the above approach:
Time Complexity: O(NxNxKx8) which is O(NxNxK), where N is the size of the board and K is the number of steps.
Space Complexity: O(NxNxK)
This article is contributed by Avinash Kumar Saw. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
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.
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Conditional Probability and Independence - Probability | Class 12 Maths
- The Knight's tour problem | Backtracking-1
- Minimum steps to reach target by a Knight | Set 1
- Warnsdorff's algorithm for Knight’s tour problem
- Possible moves of knight
- Minimum steps to reach target by a Knight | Set 2
- Count of all possible ways to reach a target by a Knight
- Puzzle | Can a Knight reach bottom from top by visiting all squares
- Maximum bishops that can be placed on N*N chessboard
- Ways to place K bishops on an N×N chessboard so that no two attack
- Check if the given chessboard is valid or not
- Total position where king can reach on a chessboard in exactly M moves
- Chessboard Pawn-Pawn game
- Check if a Queen can attack a given cell on chessboard
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Maximum non-attacking Knights that can be placed on an N*M Chessboard
- Maximum non-attacking Rooks that can be placed on an N*N Chessboard
- Puzzle | Minimum colors required to color a chessboard with no two given pair threatening each other
- Find position of non-attacking Rooks in lexicographic order that can be placed on N*N chessboard