hdu 4146 Flip Game

2023-02-24,,

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
Hzwu@SWJTU
 
Source
2011百校联动“菜鸟杯”程序设计公开赛
 
Recommend
lcy   |   We have carefully selected several similar
problems for you:  4145 4143 4147 4148 4144 
 
水题,不过很容易超时,不能完全暴力,思想是记录每行每列的地板变换了多少次,偶数则不改变,奇数则改变。
 
题意:给定一个矩阵,包含黑白两种地砖,每次选定一块地砖,将这块地砖所在的行,列的所有地砖转换颜色(黑变白,白变黑);问最后白色地砖有多少块。
 
附上代码:
 

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char ch[][];
int a[],b[];
int main()
{
int w,T,n,m,t,x,y,i,j,k;
scanf("%d",&T);
for(w=; w<=T; w++)
{
scanf("%d",&n);
for(i=; i<n; i++)
scanf("%s",ch[i]);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&x,&y);
x--,y--; //注意输入从(1,1)开始
a[x]++; //记录每一行变换的次数
b[y]++; //记录每一列变换的次数
if(a[x]==) //出现2,则表示变换2次,也就是没变,所以0表示不变,1表示变
a[x]=;
if(b[y]==)
b[y]=;
}
int s=;
for(i=; i<n; i++)
for(j=; j<n; j++)
{
if(a[i]+b[j]==) //只有出现1才是变换了,0或2都是保持不变
{
if(ch[i][j]=='b')
s++;
}
else
{
if(ch[i][j]=='w')
s++;
}
}
printf("Case #%d: %d\n",w,s);
}
return ;
}

hdu 4146 Flip Game的相关教程结束。

《hdu 4146 Flip Game.doc》

下载本文的Word格式文档,以方便收藏与打印。