-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmatrix_ofmaxsum.cpp
81 lines (81 loc) · 1.46 KB
/
submatrix_ofmaxsum.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
73
74
75
76
77
78
79
80
81
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,m,grid[100][100];
void computesum(int s)
{
int dp[n+1][m+1]={0};
int i,j,k,sum=0,f=0,c,count=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
dp[i+1][j+1]=dp[i+1][j]+dp[i][j+1]-dp[i][j]+grid[i][j];
}
}
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
cout<<dp[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n";
if(n<m)
{
count=n;
}
else
{
count=m;
}
for(k=1;k<=count;k++)
{
f=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i+k<=n && j+k<=m)
{
sum=dp[i+k][j+k]-dp[i+k][j]-dp[i][j+k]+dp[i][j];
if(sum < s)
{
f=1;
c=k;
break;
}
}
else
{
break;
}
}
if(f==1)
{
break;
}
}
if(i==n && f==0)
{
break;
}
}
cout<<c*c;
}
int main()
{
int i,j,s;
cin>>n>>m>>s;
grid[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>grid[i][j];
}
}
computesum(s);
return 0;
}