博主最近在刷蓝桥杯的真题,也是第一次打蓝桥杯,看到这道题第一眼只能想到这种做法,暴力统计每个子矩阵的值判断,时间复杂度是O(n4),在C语言网上过差不多三分之一的测试点,希望能给没有思路的小伙伴一点启发,正解的话可以看其他大佬。
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 510;
const int M = 510;
int martix[N][M];//矩阵数组
int res = 0;//结果
int k;
bool check(int lx, int ly, int rx, int ry)//检查左端点和右端点组成的举证是否能满足条件
{
int sum = 0;
if (lx > rx || ly > ry)return false;//优化,避免重复
for (int i = lx; i <= rx; i++)
{
for (int j = ly; j <= ry; j++) {
sum += martix[i][j];
}
}
if (sum > k)return false;
return true;
}
void solve()
{
int n, m;
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> martix[i][j];
}
}
//遍历每一个子矩阵
for (int lx = 0; lx < n; lx++)
{
for (int ly = 0; ly < m; ly++)
{
for (int rx = 0; rx < n; rx++)
{
for (int ry = 0; ry < m; ry++)
{
if (check(lx, ly, rx, ry))res++;
}
}
}
}
cout << res << endl;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--)
solve();
return 0;
}