-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaxSqWithZeros.cpp
More file actions
87 lines (75 loc) · 1.4 KB
/
maxSqWithZeros.cpp
File metadata and controls
87 lines (75 loc) · 1.4 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
using namespace std;
int findMaxSquareWithAllZeros(int** arr, int row, int col){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int **dp = new int*[row];
for(int i = 0; i < row; i++)
{
dp[i] = new int[col];
}
for(int i = 0; i < row; i++)
{
dp[i][0] = arr[i][0] == 1 ? 0 : 1;
}
for(int i = 0; i < col; i++)
{
dp[0][i] = arr[0][i] == 1 ? 0 : 1;
}
for(int i = 1; i < row; i++)
{
for(int j = 1; j < col; j++)
{
if(arr[i][j] == 0)
{
dp[i][j] = min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j])) + 1;
}
else
{
dp[i][j] = 0;
}
}
}
int maximum = 0;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
maximum = max(maximum, dp[i][j]);
}
}
for(int i = 0; i < row; i++)
{
// for(int j = 0; j < col; j++)
// {
// cout << dp[i][j] << " ";
// }
// cout << endl;
delete [] dp[i];
}
delete [] dp;
return maximum;
}
int main()
{
int **arr,n,m,i,j;
cin>>n>>m;
arr=new int*[n];
for(i=0;i<n;i++)
{
arr[i]=new int[m];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>arr[i][j];
}
}
cout << findMaxSquareWithAllZeros(arr,n,m) << endl;
delete arr;
return 0;
}