CodeForces 549D (暴力、模拟)

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 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 ofW - B, where W 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, the j-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 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 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 and 8-th column with coefficient1 to the variable value (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 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 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值