-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path10502-Counting Rectangles.cpp
35 lines (33 loc) · 1.01 KB
/
10502-Counting Rectangles.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m,v;
char grid[101];
while(cin >> n, n){
cin >> m;
vector<vector<int>> dpSum(n+1,vector<int>(m+1));
for(int i=1;i<=n;i++) {
scanf("%s",grid);
for(int j=1;j<=m;j++){
dpSum[i][j] = dpSum[i-1][j] + dpSum[i][j-1] - dpSum[i-1][j-1];
dpSum[i][j] += (grid[j-1]-'0');
}
}
int res = 0;
//foreach starting coordinate topleft
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
//foreach ending coordinate btmright
for(int k=i;k<=n;k++){
for(int l=j;l<=m;l++){
int fullSquare = (k-i+1)*(l-j+1);
int filledAmt = dpSum[k][l] - (dpSum[k][j-1] + dpSum[i-1][l] - dpSum[i-1][j-1]);
if(fullSquare == filledAmt) res++;
}
}
}
}
cout << res << endl;
}
}