Codeforces looksery Cup D. Haar Features

Source: Internet
Author: User

The first algorithm for detecting a face on the image working in realtime is developed by Paul Viola and Michael Jones in 2001. A part of the algorithm are A procedure that computes Haar features. As part of the this task, we consider a simplified model of this concept.

Let's consider a rectangular image that's represented with a table of size n? x? M.The table elements is integers that specify the brightness of each pixel in the image.

A feature also is a rectangular table of size n? x? M.Each cell of a feature was painted black or white.

To calculate the value of the given feature in 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 all Pixel is E Ntirely covered with either black or white cell. Thevalueof a feature in the image is the value of W?-? B , whereWis the brightness of the pixels in the image, covered with white feature cells, andBBrightness of the pixels covered with black feature cells.

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

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

A Prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left co Rner 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 the space-separated integers n and m (1?≤? N,? m≤?100)-the number of rows and columns in the feature.

NextNLines contain the description of the feature. Each line consists ofmCharacters, theJ-th character of theI-th line equals to "W"If this element of the feature are white and"B"If it is black.

Output

Print a single number-the minimum number of operations so 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 Size 6?x?8  equals to the difference of the total brightness of the pixels in the lower and Uppe R 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 would be included with factor 1, and all pixels in the U Pper three rows of the image would be included with factor 1?-? 2?=?? -?1, as required.

At first Test instructions has been not understand, later read the puzzle, found quite simple = =. The problem is from the lower right corner of the left enumeration point, if it is w, it becomes 1, B becomes-1. Note the update should start at the top left corner.

#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;}


Codeforces looksery Cup D. Haar Features

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.