-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_using_matrix_space_1.cpp
77 lines (69 loc) · 2.14 KB
/
AC_using_matrix_space_1.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_using_matrix_space_1.cpp
* Create Date: 2014-12-28 14:27:45
* Descripton: use one row and colume in matrix to record them
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if (matrix.empty() || matrix[0].empty())
return;
int n = matrix.size(), m = matrix[0].size();
int recrow = -1, reccol = -1;
// record
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!matrix[i][j]) {
// find out the first position to record
if (recrow == -1) {
recrow = i;
reccol = j;
// set the row and col be 0 if they are no 0
for (int k = 0; k < n; k++)
matrix[k][reccol] = !matrix[k][reccol];
for (int k = 0; k < m; k++)
matrix[recrow][k] = !matrix[recrow][k];
} else if (recrow != i && reccol != j) {
matrix[i][reccol] = 1;
matrix[recrow][j] = 1;
}
}
if (recrow == -1)
return;
for (int i = 0; i < n; i++) {
if (i == recrow)
continue;
for (int j = 0; j < m; j++) {
if (j == reccol)
continue;
if (matrix[i][reccol] || matrix[recrow][j])
matrix[i][j] = 0;
}
}
// set the row and col be 0
for (int k = 0; k < n; k++)
matrix[k][reccol] = 0;
for (int k = 0; k < m; k++)
matrix[recrow][k] = 0;
}
};
int main() {
int n, m;
Solution s;
cin >> n >> m;
vector<vector<int> > mat(n, vector<int>(m));
for (auto &i : mat)
for (auto &j : i)
cin >> j;
s.setZeroes(mat);
for (auto &i : mat) {
for (auto &j : i)
cout << j << ' ';
cout << endl;
}
return 0;
}