/
p1-7.cpp
101 lines (95 loc) · 1.71 KB
/
p1-7.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
91
92
93
94
95
96
97
98
99
100
101
// 1.7 Write an algorithm such that if an element in an MxN matrx is 0, its antire row and column are set to 0.
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
void setMatrixZero(vector<vector<int> > &board) {
int m, n;
m = (int)board.size();
if (m == 0) {
return;
}
n = (int)board[0].size();
if (n == 0) {
return;
}
int i, j;
bool row0_has_zero = false;
bool col0_has_zero = false;
for (i = 0; i < m; ++i) {
if (board[i][0] == 0) {
col0_has_zero = true;
break;
}
}
for (j = 0; j < n; ++j) {
if (board[0][j] == 0) {
row0_has_zero = true;
}
}
for (i = 1; i < m; ++i) {
for (j = 1; j < n; ++j) {
if (board[i][j] == 0) {
board[i][0] = 0;
board[0][j] = 0;
}
}
}
for (i = 1; i < m; ++i) {
if (board[i][0] == 0) {
for (j = 1; j < n; ++j) {
board[i][j] = 0;
}
}
}
for (j = 1; j < n; ++j) {
if (board[0][j] == 0) {
for (i = 1; i < m; ++i) {
board[i][j] = 0;
}
}
}
if (row0_has_zero) {
for (j = 0; j < n; ++j) {
board[0][j] = 0;
}
}
if (col0_has_zero) {
for (i = 0; i < m; ++i) {
board[i][0] = 0;
}
}
}
};
int main()
{
vector<vector<int> > board;
int i, j;
int m, n;
Solution sol;
while (scanf("%d%d", &m, &n) == 2 && (m || n)) {
board.resize(m);
for (i = 0; i < m; ++i) {
board[i].resize(n);
}
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
scanf("%d", &board[i][j]);
}
}
sol.setMatrixZero(board);
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
if (j > 0) {
printf(" %d", board[i][j]);
} else {
printf("%d", board[i][j]);
}
}
printf("\n");
}
printf("\n");
}
return 0;
}