-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path542.cpp
36 lines (35 loc) · 989 Bytes
/
542.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
// bruteForce.cpp
class Solution {
int row, col;
bool valid(int x, int y) {
if (x < 0 || x >= row || y < 0 || y >= col)
return 0;
return 1;
}
int bruteForce(vector<vector<int>> &matrix, int i, int j) {
int len = 1;
while (1) {
int x = 0, y = len;
while (0 <= y) {
if ((valid(i + x, j + y) && matrix[i + x][j + y] == 0) ||
(valid(i - x, j + y) && matrix[i - x][j + y] == 0) ||
(valid(i + x, j - y) && matrix[i + x][j - y] == 0) ||
(valid(i - x, j - y) && matrix[i - x][j - y] == 0))
return len;
x++, y--;
}
++len;
}
}
public:
vector<vector<int>> updateMatrix(vector<vector<int>> &matrix) {
row = matrix.size();
col = matrix[0].size();
vector<vector<int>> res(row, vector<int>(col, 0));
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j)
if (matrix[i][j])
res[i][j] = bruteForce(matrix, i, j);
return res;
}
};