-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
MaximumSubSquare.cpp
54 lines (49 loc) · 1.14 KB
/
MaximumSubSquare.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
/*
Problem: Given a binary matrix, find out the maximum size square sub-matrix with all 1s.
*/
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int min(int x,int y,int z){
return x<((y<z)?y:z)?x:((y<z)?y:z);
}
int maxSquare(vector< vector<int> > &matrix,int row,int col) {
vector< vector<int> > temp(row, vector<int>(col, 0));
int max = 0;
for(int k=0;k<matrix.size();k++)
{
temp[k][0]=matrix[k][0];
if(temp[k][0]>max)
max=temp[k][0];
}
for(int k=0;k<matrix[0].size();k++)
{
temp[0][k]=matrix[0][k];
if(temp[0][k]>max)
max=temp[k][0];
}
for(int r=1;r<matrix.size();r++){
for(int c=1;c<matrix[r].size();c++){
if(matrix[r][c]==1){
temp[r][c]=min(temp[r][c-1],temp[r-1][c-1],temp[r-1][c])+1;
if(temp[r][c]>max)
max=temp[r][c];
}
}
}
return max;
}
int main(){
int row,col;
cin>>row>>col;
vector< vector<int> > matrix(row, vector<int>(col, 0));
//input for the matrix
for(int r=0;r<matrix.size();r++){
for(int c=0;c<matrix[r].size();c++){
cin>>matrix[r][c];
}
}
int maxSubSquare=maxSquare(matrix,row,col);
cout<<"Max Sub Square: "<<maxSubSquare<<" x "<<maxSubSquare<<endl;
}