
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum mirrors which can transfer light from bottom to right in C++
We are given with a square matrix which contains 0’s and 1’s only. 0 represents a blank or empty place and 1 means obstacle. We must find a number of mirrors that can be placed at empty cells such that these mirrors can transfer light from bottom to right. This is possible when, mirror is placed at index [i,j] and for all cells on the right in that particular row ( i ) and cells in the bottom ( j ) in that particular column have no obstacle.
If the mirror is at A[i][j], then all A[i+1 to n][ j ] and A[ i ][ j+1 to n ] are empty i.e, 0. As shown in below figure.
Input
Arr[][] = {{0,0,1,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,1,1,0,1},{1,1,1,0,1}}
Output
No. of mirrors : 3
Explanation − As shown in figure. Mirrors can be placed at cells
Arr[1][0] − row 1 and column 0 has all 0’s below and right of it.
Arr[2][0] − row 2 and column 0 has all 0’s below and right of it.
Arr[4][4] − Last cell, is 0 and no rows below and column on right.
Approach used in the below program is as follows
Array Arr[][] is used to represent matrix of 0’s and 1’s..
Function maximumMirror(int mat[][], int n) takes matrix and it’s side n as input and returns the count of maximum mirrors that can be placed as shown above.
Variable flag is used to mark the presence of an obstacle in cells below or cells on the right of arr [i] [j].
Count is used to represent the number of mirrors, initially 0 .
Traverse matrix from index 0,0.
For each cell if it is empty (mirror can be placed), check for cells below( k=i+1 to n-1) . If any arr[k][j] is an obstacle (value=1 ) then break while loop and mark flag as 1. If not, continue checking for cells in right ( l=j+1 to n-1 ).
Set flag in case an obstacle is present.
After both while loops if flag is 0 then increment count as mirror can be placed.
Return count as no. of mirrors that is maximum.
Example
// C++ program to find how many mirrors can transfer // light from bottom to right #include <bits/stdc++.h> using namespace std; // method returns number of mirror which can transfer // light from bottom to right int maximumMirror(int mat[5][5], int N){ // to mark that all cells in the right or bottom are 0---no obstacle int flag=0; int count=0; //count of mirrors int i,j,k,l; //for all cells for (int i=0; i<N; i++) for(j=0;j<N;j++){ //check from next column and next row int k=i+1; int l=j+1; if(mat[i][j]==0) //position for mirror{ while(k<N) //check for rows below{ if(mat[k][j]==1) //keeping column fixed, if there is obstacle break{ flag=0; break; } else flag=1; k++; } if(flag==1) //if no obstacle in rows then check columns in right while(l<N) //checking for columns in right{ if(mat[i][l]==1) //keep row fixed, if obstacle break{ flag=0; break; } else flag=1; l++; } if(flag==1) //there is no obstacle for mirror mat[i][j] count++; } } return count; } int main(){ int N = 5; //matrix where 1 represents obstacle form 5X5 matrix int mat[5][5] = {{0,0,1,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,1,1,0,1},{1,1,1,0,1}}; cout <<"Maximum mirrors which can transfer light from bottom to right :"<< maximumMirror(mat, N) << endl; return 0; }
Output
Maximum mirrors which can transfer light from bottom to right :3
- Related Articles
- Maximum points from top left of matrix to bottom right and return back in C++
- Maximum sum path in a matrix from top to bottom in C++
- Maximum sum path in a matrix from top to bottom in C++ Program
- Maximum sum path in a matrix from top to bottom and back in C++ Program
- Print all palindromic paths from top left to bottom right in a matrix in C++
- Program to find number of ways we can reach from top left point to bottom right point in Python
- Program to find number of coins we can pick from topleft to bottom-right cell and return in Python
- Print all possible paths from top left to bottom right of a mXn matrix in C++
- Count all possible paths from top left to bottom right of a mXn matrix in C++
- Find maximum points which can be obtained by deleting elements from array in C++
- Maximum Power Transfer Theorem
- Print all paths from top left to bottom right in a matrix with four moves allowed in C++
- Maximum Points You Can Obtain from Cards in C++
- Maximum Candies You Can Get from Boxes in C++
- Maximum trains for which stoppage can be provided in C++
