-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargest-1-bordered-square.cpp
42 lines (38 loc) · 1.05 KB
/
largest-1-bordered-square.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
class Solution {
public:
vector<vector<int>> b;
int row,col;
int largest1BorderedSquare(vector<vector<int>>& grid) {
b=grid;
row=b.size();
if(row==0) return 0;
col=b[0].size();
int res=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
for(int k=1;(i+k-1<row && j+k-1<col);k++){
// cout<<i<<" "<<j<<" "<<k<<" "<<check(i,j,k)<<endl;
if(check(i,j,k)){
res=max(res,k);
}
}
}
}
// return res;
return res*res;
}
bool check(int x,int y, int l){
int xx=x+l-1;
int yy=y+l-1;
if(xx>=row || yy>=col) return false;
for(int i=x;i<=xx;i++){
if(b[i][y]!=1) return false;
if(b[i][yy]!=1) return false;
}
for(int j=y;j<=yy;j++){
if(b[x][j]!=1) return false;
if(b[xx][j]!=1) return false;
}
return true;
}
};