问题描述
输入与输出样例
输入样例1
4 16 1 6
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
输出样例1
7
输入样例2
11 8 2 2
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 7 0 0 0 7 0 0 7 7 0
7 0 7 0 7 0 7 0 7 0 7
7 0 0 0 7 0 0 0 7 0 7
7 0 0 0 0 7 0 0 7 7 0
7 0 0 0 0 0 7 0 7 0 0
7 0 7 0 7 0 7 0 7 0 0
0 7 0 0 0 7 0 0 7 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
输出样例2
83
代码思路:动态规划
可以明显地看出暴力解有很多重复计算
,但是一下子却又没有直接的动态规划的思路。
不妨先由动态规划得到一个二维数组DP_PixelSum。DP_PixelSum保存从(1,1)到(i,j)的矩形像素区域的像素值的和。
DP_PixelSum实现的思路可以参考:动态规划DP:计算二维数组从(1,1)到(i,j)的矩形区域的值的和。
那么思路就出现了:对于每个点,计算领域
的边界xLeftEdge、xRightEdge、yLeftEdge、yRightEdge。由此可以得到领域中像素点的个数PixoCount。
此外,借助DP_PixelSum,我们可以直接计算出领域中所有像素点的像素值之和:
DP_PixelSum[xRightEdge][yRightEdge] - DP_PixelSum[xLeftEdge - 1][yRightEdge] - DP_PixelSum[xRightEdge][yLeftEdge - 1] + DP_PixelSum[xLeftEdge - 1][yLeftEdge - 1];
- DP_PixelSum[xRightEdge][yRightEdge]
- DP_PixelSum[xRightEdge][yLeftEdge - 1]
- DP_PixelSum[xLeftEdge - 1][yRightEdge]
- DP_PixelSum[xLeftEdge - 1][yLeftEdge - 1]
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
#define MaxSize 610
int PixelValue[MaxSize][MaxSize] = { 0 };
int DP_PixelSum[MaxSize][MaxSize] = { 0 };
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, ValueLimition, Range, AverageLimition;
int PixelInDark = 0;
cin >> n >> ValueLimition >> Range >> AverageLimition;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
cin >> PixelValue[i][j];
}
}
//DP_PixoSum[i][j]表示从(1,1)到(i,j)的矩形区域的值的和
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
DP_PixelSum[i][j] = DP_PixelSum[i - 1][j] + DP_PixelSum[i][j - 1] - DP_PixelSum[i - 1][j - 1] + PixelValue[i][j];
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
int xLeftEdge = max(1, i - Range);
int xRightEdge = min(i + Range, n);
int yLeftEdge = max(1, j - Range);
int yRightEdge = min(j + Range, n);
int PixoCount = (xRightEdge - xLeftEdge + 1) * (yRightEdge - yLeftEdge + 1);
int TempSum = DP_PixelSum[xRightEdge][yRightEdge] - DP_PixelSum[xLeftEdge - 1][yRightEdge] - DP_PixelSum[xRightEdge][yLeftEdge - 1] + DP_PixelSum[xLeftEdge - 1][yLeftEdge - 1];
double Result = static_cast<double> (TempSum) / PixoCount;
if (Result <= AverageLimition) ++PixelInDark;
}
}
cout << PixelInDark << endl;
return 0;
}