We are given a matrix that contains different values in its each cell. Our aim is to find the minimal set of positions in the matrix such that entire matrix can be traversed starting from the positions in the set.
We can traverse the matrix under below conditions:
- We can move only to those neighbors that contain value less than or to equal to the current cell’s value. A neighbor of cell is defined as the cell that shares a side with the given cell.
Input : 1 2 3 2 3 1 1 1 1 Output : 1 1 0 2 If we start from 1, 1 we can cover 6 vertices in the order (1, 1) -> (1, 0) -> (2, 0) -> (2, 1) -> (2, 2) -> (1, 2). We cannot cover the entire matrix with this vertex. Remaining vertices can be covered (0, 2) -> (0, 1) -> (0, 0). Input : 3 3 1 1 Output : 0 1 If we start from 0, 1, we can traverse the entire matrix from this single vertex in this order (0, 0) -> (0, 1) -> (1, 1) -> (1, 0). Traversing the matrix in this order satisfies all the conditions stated above.
From the above examples, we can easily identify that in order to use minimum number of positions we have to start from the positions having highest cell value. Therefore we pick the positions that contain the highest value in the matrix. We take the vertices having highest value in separate array. We perform DFS on every vertex starting from the highest value. If we encounter any unvisited vertex during dfs then we have to include this vertex in our set. When all the cells have been processed then the set contains the required vertices.
How does this work?
We need to visit all vertices and to reach largest values we must start with them. If two largest values are not adjacent, then both of them must be picked. If two largest values are adjacent, then any of them can be picked as moving to equal value neighbors is allowed.
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 cost to traverse from one index to another in the String
- Minimum number of edges between two vertices of a graph using DFS
- Maximum and minimum isolated vertices in a graph
- Minimum Operations to make value of all vertices of the tree Zero
- Minimum number of edges between two vertices of a Graph
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Minimum possible modifications in the matrix to reach destination
- Minimum time required to fill the entire matrix with 1's
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Shortest path to traverse all the elements of a circular array in increasing order
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Find the minimum number of moves needed to move from one cell of matrix to another
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Count all possible paths between two vertices
- Shortest paths from all vertices to a destination
- Construct a graph from given degrees of all vertices
- Articulation Points (or Cut Vertices) in a Graph
- Finding in and out degrees of all vertices in a graph
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AjaySingh6