CCF-CSP-202104-2:邻域均值

问题描述

CCF-CSP-202104-2:邻域均值

输入与输出样例

输入样例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。
1
       此外,借助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]
    2
  • DP_PixelSum[xRightEdge][yLeftEdge - 1]
    3
  • DP_PixelSum[xLeftEdge - 1][yRightEdge]
    4
  • DP_PixelSum[xLeftEdge - 1][yLeftEdge - 1]
    5

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;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值