Given a binary N x N matrix, we need to find the total number of matrix positions from which there is an endless path. Any position (i, j) is said to have an endless path if and only if the position (i,j) has the value 1 and all of the next positions in its row(i) and its column(j) should have value 1. If any position next to (i,j) either in row(i) or in column(j) will have 0 then position (i,j) doesn’t have any endless path.
Input : 0 1 0 1 1 1 0 1 1 Output : 4 Endless points are (1, 1), (1, 2), (2, 1) and (2, 2). For all other points path to some corner is blocked at some point. Input : 0 1 1 1 1 0 0 1 0 Output : 1 Endless point is (0, 2).
Naive Approach :
We traverse all positions, for every position, we check that does this position has endless path or not. If yes then count it otherwise ignore it. But as usual its time complexity seems to be high.
Time complexity : O(n3)
Also, we can say that any position (i,j) will have an endless row if (i,j+1) will have an endless row and value of (i,j) is 1.
Similarly, we can say that any position (i,j) will have an endless column if (i+1,j) will have an endless column and value of (i,j) is 1.
So we should maintain two matrices one for row and one for column. Always start from right most position for row and bottom most position for column and only check for next position whether it has endless path or not.
And Finally, if any position will have an endless path in both row and column matrix then that position is said to have an endless path.
This article is contributed by Shivam Pradhan (anuj_charm). 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.
- Number of cells in matrix which are equidistant from given two points
- Minimum number of points required to cover all blocks of a 2-D grid
- Collect maximum points in a grid using two traversals
- Minimum Initial Points to Reach Destination
- Maximum points collected by two persons allowed to meet once
- Maximum points from top left of matrix to bottom right and return back
- Find row number of a binary matrix having maximum number of 1s
- Find the row with maximum number of 1s
- Find the number of islands | Set 1 (Using DFS)
- Find number of transformation to make two Matrix Equal
- Python map function to find row with maximum number of 1's
- Find the number of distinct islands in a 2D matrix
- Find number of Positional Elements
- Find number of cavities in a matrix
- Find alphabet in a Matrix which has maximum number of stars around it
- Find row with maximum and minimum number of zeroes in given Matrix
- Find the number of p-sided squares in a grid with K blacks painted
- Find the number of paths of length K in a directed graph
- Find the number of cells in the table contains X
- Find Number of Even cells in a Zero Matrix after Q queries