Skip to content

Instantly share code, notes, and snippets.

@Abhey
Created December 10, 2017 10:32
Show Gist options
  • Save Abhey/67556e4b8ad71f0c43993b782bf7f2b5 to your computer and use it in GitHub Desktop.
Save Abhey/67556e4b8ad71f0c43993b782bf7f2b5 to your computer and use it in GitHub Desktop.
Naive implementation of maximum size square sub-matrix with all 1's
#include <bits/stdc++.h>
using namespace std;
int findMaxOnesMatrixSize(vector<vector<int> > &matrix){
int n = matrix.size();
int m = matrix[0].size();
int maxOnesMatrixSize = 0;
for(int a = 1 ; a <= max(m,n) ; a ++){
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < m ; j ++){
int temp = 1;
if(i + a > n || j + a > m)
continue;
for(int x = i ; x < i + a; x ++)
for(int y = j ; y < j + a ; y ++)
if(matrix[x][y] == 0){
temp = 0;
break;
}
if(temp)
maxOnesMatrixSize = max(maxOnesMatrixSize,a);
}
}
return maxOnesMatrixSize;
}
// Driver function .............
int main(){
int test;
cin >> test;
while(test --){
int n,m;
cin >> n >> m;
vector<vector<int> > matrix(n,vector<int> (m));
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < m ; j ++)
cin >> matrix[i][j];
cout << findMaxOnesMatrixSize(matrix) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment