Number of pair of positions in matrix which are not accessible
Last Updated :
25 Apr, 2023
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).
Examples:
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:
C++
#include<bits/stdc++.h>
using namespace std;
void dfs(vector< int > graph[], bool visited[],
int x, int *k)
{
for ( int i = 0; i < graph[x].size(); i++)
{
if (!visited[graph[x][i]])
{
(*k)++;
visited[graph[x][i]] = true ;
dfs(graph, visited, graph[x][i], k);
}
}
}
int countNonAccessible(vector< int > graph[], int N)
{
bool visited[N*N + N];
memset (visited, false , sizeof (visited));
int ans = 0;
for ( int i = 1; i <= N*N; i++)
{
if (!visited[i])
{
visited[i] = true ;
int k = 1;
dfs(graph, visited, i, &k);
ans += k * (N*N - k);
}
}
return ans;
}
void insertpath(vector< int > graph[], int N, int x1,
int y1, int x2, int y2)
{
int a = (x1 - 1) * N + y1;
int b = (x2 - 1) * N + y2;
graph[a].push_back(b);
graph[b].push_back(a);
}
int main()
{
int N = 2;
vector< int > graph[N*N + 1];
insertpath(graph, N, 1, 1, 1, 2);
insertpath(graph, N, 1, 2, 2, 2);
cout << countNonAccessible(graph, N) << endl;
return 0;
}
|
Java
import java.util.*;
@SuppressWarnings ( "unchecked" )
class GFG {
static int k;
static void dfs(Vector<Integer> graph[],
boolean visited[], int x)
{
for ( int i = 0 ; i < graph[x].size(); i++) {
if (!visited[graph[x].get(i)]) {
(k)++;
visited[graph[x].get(i)] = true ;
dfs(graph, visited, graph[x].get(i));
}
}
}
static int countNonAccessible(Vector<Integer> graph[],
int N)
{
boolean [] visited = new boolean [N * N + N];
int ans = 0 ;
for ( int i = 1 ; i <= N * N; i++) {
k = 0 ;
if (!visited[i]) {
visited[i] = true ;
k++;
dfs(graph, visited, i);
ans += k * (N * N - k);
}
}
return ans;
}
static void insertpath(Vector<Integer> graph[], int N,
int x1, int y1, int x2, int y2)
{
int a = (x1 - 1 ) * N + y1;
int b = (x2 - 1 ) * N + y2;
graph[a].add(b);
graph[b].add(a);
}
public static void main(String args[])
{
int N = 2 ;
Vector<Integer>[] graph = new Vector[N * N + 1 ];
for ( int i = 1 ; i <= N * N; i++)
graph[i] = new Vector<Integer>();
insertpath(graph, N, 1 , 1 , 1 , 2 );
insertpath(graph, N, 2 , 1 , 2 , 2 );
System.out.println(countNonAccessible(graph, N));
}
}
|
Python3
def dfs(graph,visited, x, k):
for i in range ( len (graph[x])):
if ( not visited[graph[x][i]]):
k[ 0 ] + = 1
visited[graph[x][i]] = True
dfs(graph, visited, graph[x][i], k)
def countNonAccessible(graph, N):
visited = [ False ] * (N * N + N)
ans = 0
for i in range ( 1 , N * N + 1 ):
if ( not visited[i]):
visited[i] = True
k = [ 1 ]
dfs(graph, visited, i, k)
ans + = k[ 0 ] * (N * N - k[ 0 ])
return ans
def insertpath(graph, N, x1, y1, x2, y2):
a = (x1 - 1 ) * N + y1
b = (x2 - 1 ) * N + y2
graph[a].append(b)
graph[b].append(a)
if __name__ = = '__main__' :
N = 2
graph = [[] for i in range (N * N + 1 )]
insertpath(graph, N, 1 , 1 , 1 , 2 )
insertpath(graph, N, 1 , 2 , 2 , 2 )
print (countNonAccessible(graph, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int k;
static void dfs(List< int > []graph,
bool []visited, int x)
{
for ( int i = 0; i < graph[x].Count; i++)
{
if (!visited[graph[x][i]])
{
(k)++;
visited[graph[x][i]] = true ;
dfs(graph, visited, graph[x][i]);
}
}
}
static int countNonAccessible(List< int > []graph,
int N)
{
bool []visited = new bool [N * N + N];
int ans = 0;
for ( int i = 1; i <= N * N; i++)
{
if (!visited[i])
{
visited[i] = true ;
int k = 1;
dfs(graph, visited, i);
ans += k * (N * N - k);
}
}
return ans;
}
static void insertpath(List< int > []graph,
int N, int x1, int y1,
int x2, int y2)
{
int a = (x1 - 1) * N + y1;
int b = (x2 - 1) * N + y2;
graph[a].Add(b);
graph[b].Add(a);
}
public static void Main(String []args)
{
int N = 2;
List< int >[] graph = new List< int >[N * N + 1];
for ( int i = 1; i <= N * N; i++)
graph[i] = new List< int >();
insertpath(graph, N, 1, 1, 1, 2);
insertpath(graph, N, 1, 2, 2, 2);
Console.WriteLine(countNonAccessible(graph, N));
}
}
|
Javascript
<script>
let k;
function dfs(graph,visited,x)
{
for (let i = 0; i < graph[x].length; i++)
{
if (!visited[graph[x][i]])
{
(k)++;
visited[graph[x][i]] = true ;
dfs(graph, visited, graph[x][i]);
}
}
}
function countNonAccessible(graph,N)
{
let visited = new Array(N * N + N);
let ans = 0;
for (let i = 1; i <= N * N; i++)
{
if (!visited[i])
{
visited[i] = true ;
let k = 1;
dfs(graph, visited, i);
ans += k * (N * N - k);
}
}
return ans;
}
function insertpath(graph,N,x1,y1,x2,y2)
{
let a = (x1 - 1) * N + y1;
let b = (x2 - 1) * N + y2;
graph[a].push(b);
graph[b].push(a);
}
let N = 2;
let graph = new Array(N * N + 1);
for (let i = 1; i <= N * N; i++)
graph[i] = [];
insertpath(graph, N, 1, 1, 1, 2);
insertpath(graph, N, 1, 2, 2, 2);
document.write(countNonAccessible(graph, N));
</script>
|
Time Complexity : O(N * N).
Auxiliary Space: O(N × N)
Share your thoughts in the comments
Please Login to comment...