UVA983 Localized Summing for Blurring【前缀和+最大子段和+DP】

Many problems in image processing and computer graphics need to process large two dimensional arrays and with modern real time applications they need to process them very fast. For instance when one wishes to blur an image a “Convolution” filter is applied, this involves nothing more than summing the values near or around a particular pixel and applying some mathematical function for instance calculating the average value. More complicated functions exist, the Gaussian Blur being quite popular. In a Gaussian Blur when summing not all pixel values have equal weights in the sum, in other words pixel weights aren’t equal - they are given a value according to a bell-shaped Gaussian curve.
    This problem involves computing local information, very similar to “blurring” an image, but a shade simpler! You will be given an N × N array of integer data and your task will be to calculate an (N − M + 1) × (N − M + 1) array of integers. Each element of this resulting matrix is the sum of the local M × M points of the original matrix.
    For instance, consider the following example for a 4 by 4 Matrix X with M = 3.
在这里插入图片描述
    The highlighted matrix elements show the points to be summed at the first element of the matrix. The first element of a matrix is the bottom left hand corner (in this diagram).
Input
The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
    The input values consist of (N × N) + 1 lines.
    The first line contains the integer values of N and M separated by a single space.
    The next N ×N lines contain the elements of the matrix, one per line, from the bottom left element to the top right one.
    The maximum value of N is 1000. M is always greater or equal to 2 and less than N.
    Each element of the matrix is a positive integer value less than 16 (0,1,…15).
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
    The output data consists of the elements of the resulting matrix plus one extra integer value, the total sum of the elements.
    The elements of the “blurred” matrix are displayed one per line, starting from the bottom left to the top right element.
    The last line contains the total sum. This sum is guaranteed to be at most a 64 bit integer.
Note: The first sample input represents the input and output data for the example described above.
Sample Input
4 3
1
2
2
1
1
0
2
2
0
2
3
3
1
2
1
3

5 2
1
0
1
0
0
0
0
1
0
0
1
1
1
1
1
0
0
1
0
0
0
0
1
0
0
Sample Output
13
17
12
18
60

1
2
2
0
2
3
3
2
2
3
3
2
0
2
2
0
29

问题链接UVA983 Localized Summing for Blurring
问题简述:(略)
问题分析:给代码不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA983 Localized Summing for Blurring */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000;
int a[N + 1][N + 1], pre[N + 1][N + 1];

int main()
{
    int n, m, line = 0;
    while(~scanf("%d%d", &n, &m)) {
        if(line) putchar('\n');
        else line = 1;

        for(int i = n; i >= 1; i--)
            for(int j = 1; j <= n; j++)
                scanf("%d", &a[i][j]);

        memset(pre, 0, sizeof(pre));
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++)
                pre[i][j] = pre[i][j - 1] + a[i][j];
            for(int j = 1; j <= n; j++)
                pre[i][j] += pre[i - 1][j];
        }

        int sum = 0;
        for(int i = n - m + 1; i >= 1; i--)
            for(int j = 1; j <= n - m + 1; j++) {
                int s = pre[i + m - 1][j + m - 1] - pre[i - 1][j + m - 1] - pre[i + m -1][j - 1] + pre[i - 1][j - 1];
                printf("%d\n", s);
                sum += s;
            }

        printf("%d\n", sum);
    }

    return 0;
}
发布了2289 篇原创文章 · 获赞 2373 · 访问量 265万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105355556