-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay21.cpp
69 lines (39 loc) · 1.1 KB
/
Day21.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
//https://leetcode.com/problems/count-square-submatrices-with-all-ones/
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int row = matrix.size();
int col = matrix[0].size();
if(row==0)return 0;
//cout << row << " " << col << endl;
int grid[500][500];
memset(grid, false, sizeof grid);
for (int i = 1; i <=row; i++) {
for (int j = 1; j <= col; j++) {
grid[i][j] = (matrix[i-1][j-1]) ;
}
}
int ans[500][500] = { 0 };
memset(ans, 0, sizeof ans);
for (int i = 2; i <= row+1; i++) {
for (int j = 2; j <= col+1; j++) {
if (grid[i-1][j-1] == 0) {
ans[i][j] = 0;
}
else if(grid[i-1][j-1]==1) {
int up = ans[i - 1][j];
int left = ans[i][j - 1];
int diago = ans[i - 1][j - 1];
ans[i][j] = min({ up ,left , diago }) + 1;
}
}
}
int res = 0;
for (int i = 1; i <= row+1; i++) {
for (int j = 1; j <= col+1; j++) {
if (ans[i][j] > 0) res += ans[i][j];
}
}
return res;
}
};