HDU 4146 flip game

Source: Internet
Author: User
Flip game

Time Limit: 15000/5000 MS (Java/others) memory limit:
65535/32768 K (Java/Others)


Problem descriptionflip 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 should 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. inputthe 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
The following Q lines contains two INTEGER (XI, Yi), represent a command (1 <= n <= 1000, 0 <= q <= 100000, 1 <= xi, yi <= N ). outputfor each case, please print the case number (beginning with 1) and the number of white pieces after sequences of commands.


Sample Input

 
24bwwwwbwwwwbwwwwb21 14 44wwwwwwwwwwwwwwwwww11 1
Sample output
Case #1: 4 case #2: 10

Ideas:

Step 1: Determine which rows and columns in the field need to be reversed and save these rows and columns to the array. (With an exception or operation), changing an even number of rows (columns) is equal to not changing; changing an odd number is equivalent to changing one (reverse ).

Step 2: Determine whether or not each bit in the field needs to be changed (using an exception or operation) by changing the number of rows and columns, and change the intersection twice, which is equivalent to no change.

Step 3: calculate the total number of 'W' after the reversal '.

CodeAs follows:

# Include <stdio. h> # include <string. h> // an N * n square to store 'B' or 'W' unsigned char field [1001] [1001]; // an N * n square, used to mark the corresponding field array. if the flag [I] [J] is 1, it is reversed, // change the character in field [I] [J] from 'B' to 'W' or 'W' to 'B'. If the flag [I] [J] is 0, // No need to change. unsigned char flag [1001] [1001]; int main () {int I, j, N, num, Count = 0, testcase, K, Q, X, Y; // The Col array is used to record whether the columns of the field need to change; the row array is used to record whether the rows of the field need to change unsigned char Col [1001], row [1001]; scanf ("% d", & testcase); While (testcase --) {memset (flag, 0, sizeof (FLAG); memset (COL, 0, sizeof (COL); memset (row, 0, sizeof (ROW); num = 0; count ++; scanf ("% d", & N ); for (I = 0; I <n; I ++) scanf ("% s", field [I]); scanf ("% d", & Q ); // result of generating the row and Col Arrays for (I = 0; I <q; I ++) {scanf ("% d", & X, & Y ); X = X-1; y = Y-1; row [x] = row [x] ^ 1; Col [y] = Col [y] ^ 1 ;} // operate the flag array to obtain the final 0 1 matrix for (I = 0; I <n; I ++) {for (k = 0; k <N; k ++) Flag [I] [k] = Flag [I] [k] ^ row [I]; for (k = 0; k <n; k ++) flag [k] [I] = Flag [k] [I] ^ Col [I];} // Based on the flag array, combined with the field array, obtain the final number of 'W' for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) {If (field [I] [J] = 'W' & flag [I] [J] = 0) | (field [I] [J] = 'B' & flag [I] [J] = 1) num ++ ;} printf ("case # % d: % d \ n", Count, num);} 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.