Given a positive integer N. Consider a matrix of N X N. No cell can be accessible from any other cell, except the given pair cell in the form of (x1, y1), (x2, y2) i.e there is a path (accessible) between (x2, y2) to (x1, y1). The task is to find the count of pairs (a1, b1), (a2, b2) such that cell (a2, b2) is not accessible from (a1, b1).
Input : N = 2 Allowed path 1: (1, 1) (1, 2) Allowed path 2: (1, 2) (2, 2) Output : 6 Cell (2, 1) is not accessible from any cell and no cell is accessible from it. (1, 1) - (2, 1) (1, 2) - (2, 1) (2, 2) - (2, 1) (2, 1) - (1, 1) (2, 1) - (1, 2) (2, 1) - (2, 2)
Consider each cell as a node, numbered from 1 to N*N. Each cell (x, y) can be map to number using (x – 1)*N + y. Now, consider each given allowed path as an edge between nodes. This will form a disjoint set of the connected component. Now, using Depth First Traversal or Breadth First Traversal, we can easily find the number of nodes or size of a connected component, say x. Now, count of non-accessible paths are x*(N*N – x). This way we can find non-accessible paths for each connected path.
Below is implementation of this approach:
Time Complexity : O(N * N).
This article is contributed by Anuj Chauhan. 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.
- Find a way to fill matrix with 1's and 0's in blank positions
- Count positions in Binary Matrix having equal count of set bits in corresponding row and column
- Number of positions with Same address in row major and column major order
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Minimum cost to cover the given positions in a N*M grid
- Find a specific pair in Matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Find pair with maximum difference in any column of a Matrix
- Pair with maximum sum in a Matrix
- Pair with maximum difference in a Matrix
- Pair with given sum in matrix
- Check if a pair with given product exists in a Matrix
- Check if a pair with given absolute difference exists in a Matrix
- Maximize number of nodes which are not part of any edge in a Graph
- Find alphabet in a Matrix which has maximum number of stars around it
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Number of cells in matrix which are equidistant from given two points
- 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