hdu 4146 Flip Game

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 4146 Flip Game相关的知识,希望对你有一定的参考价值。

Flip Game

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1800    Accepted Submission(s): 589


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
wwww
wwww
wwww
wwww
1
1 1
 

 

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

 

Author
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  4145 4143 4147 4148 4144 
 
水题,不过很容易超时,不能完全暴力,思想是记录每行每列的地板变换了多少次,偶数则不改变,奇数则改变。
 
题意:给定一个矩阵,包含黑白两种地砖,每次选定一块地砖,将这块地砖所在的行,列的所有地砖转换颜色(黑变白,白变黑);问最后白色地砖有多少块。
 
附上代码:
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 char ch[1005][1005];
 6 int a[1005],b[1005];
 7 int main()
 8 {
 9     int w,T,n,m,t,x,y,i,j,k;
10     scanf("%d",&T);
11     for(w=1; w<=T; w++)
12     {
13         scanf("%d",&n);
14         for(i=0; i<n; i++)
15             scanf("%s",ch[i]);
16         memset(a,0,sizeof(a));
17         memset(b,0,sizeof(b));
18         scanf("%d",&m);
19         while(m--)
20         {
21             scanf("%d%d",&x,&y);
22             x--,y--;     //注意输入从(1,1)开始
23             a[x]++;      //记录每一行变换的次数
24             b[y]++;       //记录每一列变换的次数
25             if(a[x]==2)    //出现2,则表示变换2次,也就是没变,所以0表示不变,1表示变
26                 a[x]=0;
27             if(b[y]==2)
28                 b[y]=0;
29         }
30         int s=0;
31         for(i=0; i<n; i++)
32             for(j=0; j<n; j++)
33             {
34                 if(a[i]+b[j]==1)   //只有出现1才是变换了,0或2都是保持不变
35                 {
36                     if(ch[i][j]==b)
37                         s++;
38                 }
39                 else
40                 {
41                     if(ch[i][j]==w)
42                         s++;
43                 }
44             }
45         printf("Case #%d: %d\n",w,s);
46     }
47     return 0;
48 }

 

以上是关于hdu 4146 Flip Game的主要内容,如果未能解决你的问题,请参考以下文章

14:Flip Game

POJ_1753——Flip Game(枚举)

LeetCode Flip Game II

POJ1753Flip Game(DFS + 枚举)

LeetCode–Flip Game

Flip Game