Given a rectangular matrix, we can move from current cell in 4 directions with equal probability. The 4 directions are right, left, top or bottom. Calculate the Probability that after N moves from a given position (i, j) in the matrix, we will not cross boundaries of the matrix at any point.
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to perform something similar to DFS. We recursively traverse in each of the 4 allowed direction and for each cell encountered, we calculate the required probability with one less move. As each direction has equal probability, each direction will contribute to 1/4 of total probability of that cell i.e. 0.25. We return 0 if we step outside the matrix and return 1 if N steps are completed without crossing matrix boundaries.
Below is the implementation of above idea :
Probability is 0.875
This article is contributed by Aditya Goel. 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 required to convert the matrix into lower hessenberg matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- A Boolean Matrix Question
- Find probability of selecting element from kth column after N iterations
- Find the probability of a state at a given time in a Markov chain | Set 1
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Program to check diagonal matrix and scalar matrix
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- Program to check if a matrix is Binary matrix or not
- C++ program to Convert a Matrix to Sparse Matrix
- Program to convert given Matrix to a Diagonal Matrix
- Check if it is possible to make the given matrix increasing matrix or not
- Convert given Matrix into sorted Spiral Matrix
- Maximum trace possible for any sub-matrix of the given matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix