CF_127E reader Display

来源:互联网 发布:js字符串去重 编辑:程序博客网 时间:2024/06/05 22:52

这道题其实找到规律之后其实不难,和破损的键盘一样,都有点递推转移的感觉


题意:

你可以进行这样一次操作:选一个点,然后把这个点横切竖切直到正对角线上面的点,全部翻转过来,问你要进行多少次操作才能把所有的点都翻转过来

思路:

多次模拟之后可以发现最右上方的点仅能是选择自己才能翻转,之后与这个相邻的点变成了两个最右上方,而这两个点和刚才那个点一样仅能由自己翻转

还有一个要注意的这个只能影响对角线一侧的

所以只要在对角线一侧一直从最右边上到下遍历翻转就好了

Description

After years of hard work scientists invented an absolutely new e-reader display. The new display has a larger resolution, consumes less energy and its production is cheaper. And besides, one can bend it. The only inconvenience is highly unusual management. For that very reason the developers decided to leave the e-readers' software to programmers.

The display is represented by n × n square of pixels, each of which can be either black or white. The display rows are numbered with integers from 1 to n upside down, the columns are numbered with integers from 1 to n from the left to the right. The display can perform commands like "x, y". When a traditional display fulfills such command, it simply inverts a color of (x, y), where x is the row number and y is the column number. But in our new display every pixel that belongs to at least one of the segments (x, x) - (x, y)and (y, y) - (x, y) (both ends of both segments are included) inverts a color.

For example, if initially a display 5 × 5 in size is absolutely white, then the sequence of commands (1, 4)(3, 5)(5, 1)(3, 3)leads to the following changes:

You are an e-reader software programmer and you should calculate minimal number of commands needed to display the picture. You can regard all display pixels as initially white.

Input

The first line contains number n (1 ≤ n ≤ 2000).

Next n lines contain n characters each: the description of the picture that needs to be shown. "0" represents the white color and "1" represents the black color.

Output

Print one integer z — the least number of commands needed to display the picture.

Sample Input

Input
50111010010100011001111110
Output
4
<pre name="code" class="cpp">#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<queue>#include<cstdlib>#include<algorithm>#include<stack>#include<map>#include<queue>#include<vector>using namespace std;char mp[2048][2048];int flagr[2048],flagc[2048];int n,cnt;void print(){    for(int i=0;i<n;i++)    cout<<mp[i]<<endl;    cout<<"*****************"<<endl;    cout<<endl;}void search(){  //  print();    for(int i=0;i<n;i++){        for(int j=n-1;j>i;j--){            if((mp[i][j]-'0'+flagr[i]+flagc[j])&1){                mp[i][j]='0';                flagc[j]++,flagr[i]++,cnt++;            }            mp[i][j]='0';        }    }       // 0 -> 1, 1 -> 0, '0' = 48;    //  (mp[i][i] - '0' + flagr[i] +flagc[i] )%2     for(int i=0;i<n;i++){        mp[i][i]=(mp[i][i] - '0' + flagr[i] +flagc[i] )%2 +48;    }    memset(flagr,0,sizeof flagr);    memset(flagc,0,sizeof flagc);    for(int i=n-1;i>=0;i--){        for(int j=0;j<i;j++){            if((mp[i][j]-'0'+flagr[i]+flagc[j])&1){             //   print();                flagc[j]++,flagr[i]++,cnt++;            }            mp[i][j]='0';        }    }    for(int i=0;i<n;i++)        if((mp[i][i] - '0' + flagr[i] +flagc[i] )%2) cnt++;}int main(){    //freopen("in.txt","r",stdin);   // freopen("out.txt","w",stdout);    cin>>n;    for(int i = 0;i < n;i++){        scanf("%s",mp[i]);    }    search();    printf("%d\n",cnt);    return 0;}


0 0
原创粉丝点击