A square matrix is given in which each cell represents either a blank or an obstacle. We can place mirrors at blank positionl. All mirrors will be situated at 45 degree, i.e. they can transfer light from bottom to right if no obstacle is there in their path.
In this question we need to count how many such mirrors can be placed in square matrix which can transfer light from bottom to right.
Output for above example is 2. In above diagram, mirror at (3, 1) and (5, 5) are able to send light from bottom to right so total possible mirror count is 2.
We can solve this problem by checking position of such mirrors in matrix, the mirror which can transfer light from bottom to right will not have any obstacle in their path i.e.
if a mirror is there at index (i, j) then
there will be no obstacle at index (k, j) for all k, i < k <= N
there will be no obstacle at index (i, k) for all k, j < k <= N
Keeping above two equations in mind, we can find rightmost obstacle at every row in one iteration of given matrix and we can find bottommost obstacle at every column in another iteration of given matrix. After storing these indices in separate array we can check at each index whether it satisfies no obstacle condition or not and then increase the count accordingly.
Below is implemented solution on above concept which requires O(N^2) time and O(N) extra space.
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.
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.
- Maximum points from top left of matrix to bottom right and return back
- Maximum sum path in a matrix from top-left to bottom-right
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Print all palindromic paths from top left to bottom right in a matrix
- Lexicographically largest prime path from top-left to bottom-right in a matrix
- Print all paths from top left to bottom right in a matrix with four moves allowed
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
- Print a matrix in alternate manner (left to right then right to left)
- Maximum sum path in a matrix from top to bottom
- Maximum sum path in a matrix from top to bottom and back
- Find maximum sum from top to bottom row with no adjacent diagonal elements