CodeForces 549D Haar Features 矩形成图

原题 http://codeforces.com/contest/549/problem/D

题目:

Haar Features
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.

Let’s consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.

A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.

To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.

Some examples of the most popular Haar features are given below.

Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.

A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.

You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values ​​at any prefix rectangle, multiply it by any integer and add to variable value.

You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.

Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.

Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to “W”, if this element of the feature is white and “B” if it is black.

Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.

Sample test(s)
input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
output
2
input
3 3
WBW
BWW
WWW
output
4
input
3 6
WWBBWW
WWBBWW
WWBBWW
output
3
input
4 4
BBBB
BBBB
BBBB
BBBW
output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:

add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame);
add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient  - 2 and variable value.
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 =  - 1, as required.

思路:

每次必须从该图的第一个点到需要改变的点做矩形,对矩形的所有元素进行同时加减一个数。
最后得到所求的图形需要最小的步骤。

一开始全为0,最后变成正负1的黑白格。
只需要暴力的从右往左,从下往上遍历一次。找到需要修改的再从头到该点统一加减即可。

代码:

#include"stdlib.h"
#include"string.h"
#include <iostream>
#include"cstdio"
using namespace std;

int main()
{
    const int bc=105;
    int m,n;
    char tu[bc][bc];
    int ans[bc][bc];
    while(scanf("%d %d",&m,&n)!=EOF)
    {
        int coun=0;
        memset(tu,0,sizeof(tu));
        memset(ans,0,sizeof(ans));
        getchar();
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%c",&tu[i][j]);
            }
            getchar();
        }
        for(int i=m;i>=1;i--)
        {
            for(int j=n;j>=1;j--)
            {
                int t=0;
                if(tu[i][j]=='W')
                {
                    t=1;
                }
                if(tu[i][j]=='B')
                {
                    t=-1;
                }
                if(ans[i][j]!=t)
                {
                    coun++;
                    int an=t-ans[i][j];
                    for(int k=1;k<=i;k++)
                    {
                        for(int l=1;l<=j;l++)
                        {
                            ans[k][l]=ans[k][l]+an;
                        }
                    }
                }
            }
        }
        printf("%d\n",coun);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值