CodeForces 549D (暴力、模拟)

来源:互联网 发布:淘宝买了假酒怎么处理 编辑:程序博客网 时间:2024/05/03 02:26
D. Haar Features
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 computesHaar 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 sizen × m. Each cell of afeature 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 ofW - B, whereW is the total brightness of the pixels in the image, covered with white feature cells, andB 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-calledprefix 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 oneoperation you can count the sum of pixel values ​​at any prefix rectangle, multiply it by any integer and add to variablevalue.

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 andm (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 ofm characters, thej-th character of thei-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 8BBBBBBBBBBBBBBBBBBBBBBBBWWWWWWWWWWWWWWWWWWWWWWWW
Output
2
Input
3 3WBWBWWWWW
Output
4
Input
3 6WWBBWWWWBBWWWWBBWW
Output
3
Input
4 4BBBBBBBBBBBBBBBW
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 size6 × 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 twooperations:

  1. add the sum of pixels in the prefix rectangle with the lower right corner in the6-th row and8-th column with coefficient1 to the variablevalue (the rectangle is indicated by a red frame);
  2. add the number of pixels in the prefix rectangle with the lower right corner in the3-rd row and8-th column with coefficient - 2 and variablevalue.

Thus, all the pixels in the lower three rows of the image will be included with factor1, and all pixels in the upper three rows of the image will be included with factor1 - 2 =  - 1, as required.



题目很长,要读好久。题目要求所有的W都是1,B都是-1,而且每次操作都要修改一个前缀矩形。由于询问的是最少的操作次数,因此初始化都是0,接下来从右下角开始扫描,如果遇到的W不是1,cnt++,同时将前缀矩形(0,0)到(i,j)的每个值都加上1-g[i][j](这样g[i][j]就是1了),用同样的方法处理遇到的B。

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;int main(){int n,m,i,j,k,l,mat[105][105],ans;char map[105][105];while(scanf("%d%d",&n,&m)!=EOF)    {    ans=0;    for(i=1;i<=n;i++)    for(j=1;j<=m;j++)    cin>>map[i][j];    memset(mat,0,sizeof(mat));    for(i=n;i>=1;i--)    for(j=m;j>=1;j--)    {    if(map[i][j]=='B'&&mat[i][j]!=1){    ans++;    int aa=1-mat[i][j];    for(k=1;k<=i;k++)    for(l=1;l<=j;l++)    mat[k][l]=mat[k][l]+aa;    }    if(map[i][j]=='W'&&mat[i][j]!=-1){    ans++;    int aa=-1-mat[i][j];    for(k=1;k<=i;k++)    for(l=1;l<=j;l++)    mat[k][l]=mat[k][l]+aa;    }    }    printf("%d\n",ans);    }}


0 0
原创粉丝点击