-
Notifications
You must be signed in to change notification settings - Fork 0
/
Boolean_Matrix.cpp
90 lines (73 loc) · 1.95 KB
/
Boolean_Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to modify the matrix such that if a matrix cell matrix[i][j]
//is 1 then all the cells in its ith row and jth column will become 1.
void markRow(int ind, int n, vector<vector<int> > &matrix)
{
for(int i = 0; i < n; i++)
matrix[ind][i] = 1;
}
void markCol(int ind, int n, vector<vector<int> > &matrix)
{
for(int i = 0; i < n; i++)
matrix[i][ind] = 1;
}
void booleanMatrix(vector<vector<int> > &matrix)
{
int n = matrix.size();
int m = matrix[0].size();
vector<int> row(n, 0);
vector<int> col(m, 0);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(matrix[i][j] == 1)
{
row[i] = 1;
col[j] = 1;
}
}
}
for(int i = 0; i < n; i++)
if(row[i] == 1) markRow(i, m, matrix);
for(int i = 0; i < m; i++)
if(col[i] == 1) markCol(i, n, matrix);
}
};
//{ Driver Code Starts.
int main() {
int t;
cin>>t;
while(t--)
{
int row, col;
cin>> row>> col;
vector<vector<int> > matrix(row);
for(int i=0; i<row; i++)
{
matrix[i].assign(col, 0);
for( int j=0; j<col; j++)
{
cin>>matrix[i][j];
}
}
Solution ob;
ob.booleanMatrix(matrix);
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}
// } Driver Code Ends