Maximum mirrors which can transfer light from bottom to right
Last Updated :
06 Jul, 2022
A square matrix is given in which each cell represents either a blank or an obstacle. We can place mirrors at blank position. 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.
Examples:
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.
C++
#include <bits/stdc++.h>
using namespace std;
int maximumMirrorInMatrix(string mat[], int N)
{
int horizontal[N], vertical[N];
memset (horizontal, -1, sizeof (horizontal));
memset (vertical, -1, sizeof (vertical));
for ( int i=0; i<N; i++)
{
for ( int j=N-1; j>=0; j--)
{
if (mat[i][j] == 'B' )
continue ;
horizontal[i] = j;
break ;
}
}
for ( int j=0; j<N; j++)
{
for ( int i=N-1; i>=0; i--)
{
if (mat[i][j] == 'B' )
continue ;
vertical[j] = i;
break ;
}
}
int res = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (i > vertical[j] && j > horizontal[i])
{
res++;
}
}
}
return res;
}
int main()
{
int N = 5;
string mat[N] = { "BBOBB" ,
"BBBBO" ,
"BBBBB" ,
"BOOBO" ,
"BBBOB"
};
cout << maximumMirrorInMatrix(mat, N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maximumMirrorInMatrix(String mat[], int N)
{
int [] horizontal = new int [N];
int [] vertical = new int [N];
Arrays.fill(horizontal, - 1 );
Arrays.fill(vertical, - 1 );
for ( int i = 0 ; i < N; i++)
{
for ( int j = N - 1 ; j >= 0 ; j--)
{
if (mat[i].charAt(j) == 'B' )
{
continue ;
}
horizontal[i] = j;
break ;
}
}
for ( int j = 0 ; j < N; j++)
{
for ( int i = N - 1 ; i >= 0 ; i--)
{
if (mat[i].charAt(j) == 'B' )
{
continue ;
}
vertical[j] = i;
break ;
}
}
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
if (i > vertical[j] && j > horizontal[i])
{
res++;
}
}
}
return res;
}
public static void main(String[] args)
{
int N = 5 ;
String mat[] = { "BBOBB" ,
"BBBBO" ,
"BBBBB" ,
"BOOBO" ,
"BBBOB"
};
System.out.println(maximumMirrorInMatrix(mat, N));
}
}
|
Python3
def maximumMirrorInMatrix(mat, N):
horizontal = [ - 1 for i in range (N)]
vertical = [ - 1 for i in range (N)];
for i in range (N):
for j in range (N - 1 , - 1 , - 1 ):
if (mat[i][j] = = 'B' ):
continue ;
horizontal[i] = j;
break ;
for j in range (N):
for i in range (N - 1 , - 1 , - 1 ):
if (mat[i][j] = = 'B' ):
continue ;
vertical[j] = i;
break ;
res = 0 ;
for i in range (N):
for j in range (N):
if (i > vertical[j] and j > horizontal[i]):
res + = 1 ;
return res;
N = 5 ;
mat = [ "BBOBB" ,
"BBBBO" ,
"BBBBB" ,
"BOOBO" ,
"BBBOB"
];
print (maximumMirrorInMatrix(mat, N));
|
C#
using System;
class GFG
{
static int maximumMirrorInMatrix(String []mat, int N)
{
int [] horizontal = new int [N];
int [] vertical = new int [N];
for ( int i = 0; i < N; i++)
{
horizontal[i]=-1;
vertical[i]=-1;
}
for ( int i = 0; i < N; i++)
{
for ( int j = N - 1; j >= 0; j--)
{
if (mat[i][j] == 'B' )
{
continue ;
}
horizontal[i] = j;
break ;
}
}
for ( int j = 0; j < N; j++)
{
for ( int i = N - 1; i >= 0; i--)
{
if (mat[i][j] == 'B' )
{
continue ;
}
vertical[j] = i;
break ;
}
}
int res = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (i > vertical[j] && j > horizontal[i])
{
res++;
}
}
}
return res;
}
public static void Main(String[] args)
{
int N = 5;
String []mat = { "BBOBB" ,
"BBBBO" ,
"BBBBB" ,
"BOOBO" ,
"BBBOB"
};
Console.WriteLine(maximumMirrorInMatrix(mat, N));
}
}
|
Javascript
<script>
function maximumMirrorInMatrix(mat, N)
{
var horizontal = Array(N).fill(-1);
var vertical = Array(N).fill(-1);
for ( var i = 0; i < N; i++)
{
for ( var j = N - 1; j >= 0; j--)
{
if (mat[i][j] == 'B' )
{
continue ;
}
horizontal[i] = j;
break ;
}
}
for ( var j = 0; j < N; j++)
{
for ( var i = N - 1; i >= 0; i--)
{
if (mat[i][j] == 'B' )
{
continue ;
}
vertical[j] = i;
break ;
}
}
var res = 0;
for ( var i = 0; i < N; i++)
{
for ( var j = 0; j < N; j++)
{
if (i > vertical[j] && j > horizontal[i])
{
res++;
}
}
}
return res;
}
var N = 5;
var mat = [ "BBOBB" ,
"BBBBO" ,
"BBBBB" ,
"BOOBO" ,
"BBBOB"
];
document.write(maximumMirrorInMatrix(mat, N));
</script>
|
Time complexity: O(n2).
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...