-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmax_subsquare_0's.cpp
72 lines (52 loc) · 1.43 KB
/
max_subsquare_0's.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
/* "https://leetcode.com/problems/maximal-square/" */
#include<bits/stdc++.h>
using namespace std;
void getMatrixData(int rows,int columns,vector<vector<int>>&matrix)
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
cin>>matrix[i][j];
}
}
}
void calSubSquare(int rows,int columns,vector<vector<int>>&matrix,vector<vector<int>>&tempMatrix)
{
for(int j=0;j<columns;j++)
tempMatrix[0][j]=(matrix[0][j]==0)?1:0;
for(int i=0;i<rows;i++)
tempMatrix[i][0]=(matrix[i][0]==0)?1:0;
for(int i=1;i<rows;i++)
{
for(int j=1;j<columns;j++)
{
tempMatrix[i][j]=(matrix[i][j]==0)?(min(tempMatrix[i-1][j-1],min(tempMatrix[i-1][j],tempMatrix[i][j-1]))+1):0;
}
}
}
int getSubSquare(int rows,int columns,vector<vector<int>>&matrix)
{
vector<vector<int>>tempMatrix(rows,vector<int>(columns));
calSubSquare(rows,columns,matrix,tempMatrix);
int maxSize=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
maxSize=max(maxSize,tempMatrix[i][j]);
}
}
return maxSize;
}
int main()
{
int rows,columns;
cin>>rows;
cout<<"Enter the number of rows"<<endl;
cin>>columns;
cout<<"Enter the number of columns"<<endl;
vector<vector<int>>matrix(rows,vector<int>(columns));
getMatrixData(rows,columns,matrix);
cout<<getSubSquare(rows,columns,matrix);
}