Boolean Matrix Problem
PROBLEM :
Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true) then make all the cells of ith row and jth column as 1.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is r and c,r is the number of rows and c is the number of columns.
The second line of each test case contains all the elements of the matrix in a single line separated by a single space.
Output:
Print the modified array.
Constraints:
1 = T = 50
1 = r,c = 20
0 = Elements of matrix = 1
Example:
Input:
3
2 2
1 0 0 0
2 3
0 0 0 0 0 1
3 4
1 0 0 1 0 0 1 0 0 0 0 0
Output:
1 1 1 0
0 0 1 1 1 1
1 1 1 1 1 1 1 1 1 0 1 1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 20
void reaarange_matrix(int [MAX][MAX],int ,int ) ;
int main()
{
int t,row,col,i,j,mat[MAX][MAX] ;
cin>>t ;
while(t--)
{
cin>>row>>col ;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>mat[i][j] ;
reaarange_matrix(mat,row,col) ;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}
return 0;
}
void reaarange_matrix(int mat[MAX][MAX],int row,int col)
{
int arr1[row]={0} ;
int arr2[col]={0} ;
int i,j ;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(mat[i][j]==1)
{
arr1[i]=1 ;
arr2[j]=1 ;
}
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(arr1[i]==1||arr2[j]==1)
mat[i][j]=1 ;
}
}
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( without using EXTRA space as ARRAY)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 20
void reaarange_matrix(int [MAX][MAX],int ,int ) ;
int main()
{
int t,row,col,i,j,mat[MAX][MAX] ;
cin>>t ;
while(t--)
{
cin>>row>>col ;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>mat[i][j] ;
reaarange_matrix(mat,row,col) ;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}
return 0;
}
void reaarange_matrix(int mat[MAX][MAX],int row,int col)
{
int i,j ;
bool r,c ;
r=false ;
c=false ;
for(i=0;i<row;i++)
if(mat[i][0]==1)
r=true ;
for(j=0;j<col;j++)
if(mat[0][j]==1)
c=true ;
for(i=1;i<row;i++)
{
for(j=1;j<col;j++)
{
if(mat[i][j]==1)
{
mat[i][0]=1 ;
mat[0][j]=1 ;
}
}
}
for(i=1;i<row;i++)
{
for(j=1;j<col;j++)
{
if(mat[i][0]==1||mat[0][j]==1)
mat[i][j]=1 ;
}
}
if(r==true)
{
for(i=0;i<row;i++)
mat[i][0]=1 ;
}
if(c==true)
{
for(j=0;j<col;j++)
mat[0][j]=1 ;
}
}
---------------------------------------------------------------------------------
Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true) then make all the cells of ith row and jth column as 1.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is r and c,r is the number of rows and c is the number of columns.
The second line of each test case contains all the elements of the matrix in a single line separated by a single space.
Output:
Print the modified array.
Constraints:
1 = T = 50
1 = r,c = 20
0 = Elements of matrix = 1
Example:
Input:
3
2 2
1 0 0 0
2 3
0 0 0 0 0 1
3 4
1 0 0 1 0 0 1 0 0 0 0 0
Output:
1 1 1 0
0 0 1 1 1 1
1 1 1 1 1 1 1 1 1 0 1 1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 20
void reaarange_matrix(int [MAX][MAX],int ,int ) ;
int main()
{
int t,row,col,i,j,mat[MAX][MAX] ;
cin>>t ;
while(t--)
{
cin>>row>>col ;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>mat[i][j] ;
reaarange_matrix(mat,row,col) ;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}
return 0;
}
void reaarange_matrix(int mat[MAX][MAX],int row,int col)
{
int arr1[row]={0} ;
int arr2[col]={0} ;
int i,j ;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(mat[i][j]==1)
{
arr1[i]=1 ;
arr2[j]=1 ;
}
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(arr1[i]==1||arr2[j]==1)
mat[i][j]=1 ;
}
}
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( without using EXTRA space as ARRAY)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 20
void reaarange_matrix(int [MAX][MAX],int ,int ) ;
int main()
{
int t,row,col,i,j,mat[MAX][MAX] ;
cin>>t ;
while(t--)
{
cin>>row>>col ;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>mat[i][j] ;
reaarange_matrix(mat,row,col) ;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}
return 0;
}
void reaarange_matrix(int mat[MAX][MAX],int row,int col)
{
int i,j ;
bool r,c ;
r=false ;
c=false ;
for(i=0;i<row;i++)
if(mat[i][0]==1)
r=true ;
for(j=0;j<col;j++)
if(mat[0][j]==1)
c=true ;
for(i=1;i<row;i++)
{
for(j=1;j<col;j++)
{
if(mat[i][j]==1)
{
mat[i][0]=1 ;
mat[0][j]=1 ;
}
}
}
for(i=1;i<row;i++)
{
for(j=1;j<col;j++)
{
if(mat[i][0]==1||mat[0][j]==1)
mat[i][j]=1 ;
}
}
if(r==true)
{
for(i=0;i<row;i++)
mat[i][0]=1 ;
}
if(c==true)
{
for(j=0;j<col;j++)
mat[0][j]=1 ;
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment