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 valueof 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 twooperations:

  1. 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);
  2. 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.

一開始题意一直看不懂,后来看了题解,发现挺简单的= = 。这题就是从右下角依次向左枚举点,假设是W,就变为1,是B就变为-1。注意更新的时候要从最左上角開始更新。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
char s[105][105];
int map[105][106];
int main()
{
int n,m,i,j,c,ans,r,num;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++){
scanf("%s",s[i]);
}
num=0;
memset(map,0,sizeof(map));
for(i=n-1;i>=0;i--){
for(j=m-1;j>=0;j--){
if(s[i][j]=='W'){
if(map[i][j]!=1){
num++;
ans=1-map[i][j];
for(r=i;r>=0;r--){
for(c=j;c>=0;c--){
map[r][c]+=ans;
}
}
}
}
else if(s[i][j]=='B'){
if(map[i][j]!=-1){
num++;
ans=map[i][j]+1;
for(r=i;r>=0;r--){
for(c=j;c>=0;c--){
map[r][c]-=ans;
}
}
}
}
}
}
printf("%d\n",num);
}
return 0;
}

最新文章

  1. Python基础篇【第3篇】: Python正则表达式
  2. 20145304 Java第八周学习报告
  3. 文件无刷新上传(swfUpload与uploadify)
  4. C++中不能声明为虚函数的有哪些函数
  5. shell 运算
  6. JS概念
  7. com.classpath.www
  8. Thinkphp twig
  9. LA 6856 Circle of digits 解题报告
  10. C# 多线程经典示例 吃苹果
  11. codeforces #309 DIV2
  12. bootstrap 基础表单 内联表单 横向表单
  13. .Net程序员学用Oracle系列(24):数据字典、死锁
  14. PHP content-type为&quot;application/json&quot;的post过来的数据$_POST接受不到的问题
  15. 解决Linux下Jexus验证码无法显示
  16. 【Visual Studio 扩展工具】如何在ComponentOne的DataTree中实现RightToLeft布局
  17. python接口自动化测试二十一:类和方法
  18. XmlHelpers
  19. Scala_模式匹配
  20. 安卓解析json

热门文章

  1. c# winform控件dock属性停造位置、摆放顺序详解
  2. MVC系列学习(十四)-路由规则及路由调试工具
  3. 6.10---mybatis中两张表查询数据dao层
  4. cordova科大讯飞语音识别
  5. POJ_3013_最短路
  6. IOS7升级攻略
  7. PAC代理语法含义与书写规范
  8. CAD绘制一个角度标注(com接口VB语言)
  9. .NET 大数据量并发解决方案
  10. SOUI界面库 添加 windows系统文件图标皮肤