Hdu 4146 Flip Game

Source: Internet
Author: User

Hdu 4146 Flip Game
Flip GameTime Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission (s): 1789 Accepted Submission (s): 585

Problem Description Flip game is played on a square N * N field with two-sided pieces placed on each of its N ^ 2 squares. one side of each piece is white and the other one is black and each piece is lying either it's black or white side up. the 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. sequences of commands ( Xi, yi) are given from input, which means that both pieces in row xi and pieces in column yi will be flipped (Note that piece (xi, yi) will be flipped twice here ). can you tell me how many white pieces after sequences of commands?
Consider the following 4*4 field as an example:

Bwww
Wbww
Wwbw
Wwwb

Here "B" denotes pieces lying their black side up and "w" denotes pieces lying their white side up.
Two commands are given in order: (1, 1), (4, 4). Then we can get the final 4*4 field as follows:

Bbbw
Bbwb
Bwbb
Wbbb

So the answer is 4 as there are 4 white pieces in the final field.

Input The first line contains a positive integer T, indicating the number of test cases (1 <=t <= 20 ).
For each case, the first line contains a positive integer N, indicating the size of field; The following N lines contain N characters each which represent the initial field. the following line contain an integer Q, indicating the number of commands; each of the following Q lines contains two integer (xi, yi ), represent a command (1 <= N <= 1000, 0 <= Q <= 100000, 1 <= xi, yi <= N ).

Output For each case, please print the case number (beginning with 1) and the number of white pieces after sequences of commands.
Sample Input

2 4 bwww wbww wwbw wwwb 2 1 1 4 4 4 4 wwww 1 1 1 

Sample Output
Case #1: 4 Case #2: 10 

Author [email protected]
Source 2011 hundred schools linked "cainiao Cup" program design Open Question: In an x * y square matrix is divided into white and black two colors, output 1, 4 indicates changing the first column and the fourth column of the first row. (If Xi (representing the X abscissa table) is equal to Yi (Y's ordinate), it is changed twice ). The final result to be output is the number of white blocks in this policy.

 

Solution: the time required for this question is extremely strict, and it has timed out for n times ~~~ Bitwise operations are required to process the number of times. (If the number of flipped requests is an even number of times, you do not need to process them because the flipped requests also need to be rolled back)

Also, enter !!! At the beginning, the time-out of character input was adopted! Input A as A string. Good luck ~~~~

For details, see the code.

 

#include 
 
  #include 
  
   #include 
   
    using namespace std;char ch[1010][1010];int vh[1010],vl[1010];int n;int main(){    int t;    int q=1;    scanf("%d",&t);    while (t--)    {        scanf("%d",&n);        memset(vh,0,sizeof(vh));        memset(vl,0,sizeof(vl));        for (int i=1; i<=n; i++)        {            scanf("%s",ch[i]+1);            //getchar();            //for (int j=1; j<=n; j++)            //{            //    ch[i][j]=getchar();            //}        }        int k;        scanf("%d",&k);        while (k--)        {            int x,y;            scanf("%d%d",&x,&y);             vh[x]^=1;            vl[y]^=1;        }        int ans=0;        for (int i=1; i<=n; i++)        {            for (int j=1; j<=n; j++)            {                if(vh[i]+vl[j]==1)                {                    if (ch[i][j]=='b')                        ans++;                }                else if (ch[i][j]=='w')                    ans++;            }        }        //return ans;        //turn();        printf ("Case #%d: %d\n",q++,ans);    }    return 0;}
   
  
 

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.