HDU4146 Flip Game
2016-02-23 20:35
274 查看
Flip Game
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1728 Accepted Submission(s): 571
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百校联动“菜鸟杯”程序设计公开赛
题意:给定一个矩阵,包含黑白两种地砖,每次选定一块地砖,将这块地砖所在的行,列的所有地砖转换颜色(黑变白,白变黑);问最后白色地砖有多少块。
分析:不能一次一次的翻转,会超时,可以先记录每行每列翻转的次数,翻转偶次相当于没翻转。
<span style="font-size:18px;">#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1000000007; #define ll long long #define CL(a,b) memset(a,b,sizeof(a)) #define MAXN 100010 int T,n; int Q,x,y,sum; char mat[1005][1005]; int a[1005],b[1005];//行翻转次数和列翻转次数 int main() { scanf("%d",&T); for(int cas=1; cas<=T; cas++) { CL(a, 0); CL(b, 0); scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%s",mat[i]); } scanf("%d",&Q); while(Q--) { scanf("%d%d",&x,&y); x--;y--; a[x] = a[x]^1; b[y] = b[y]^1; } sum = 0; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(a[i]+b[j] == 1) {if(mat[i][j] == 'b') sum++;} else {if(mat[i][j] == 'w') sum++;} } } printf("Case #%d: %d\n",cas,sum); } return 0; }</span>
Ps:经过一个寒假发现好多东西都不熟练了,还是得多多练习,不能太松懈了。
相关文章推荐
- ps2
- tarjan求强联通分量
- Universal-Image-Loader(UIL)图片加载框架使用简单介绍
- YTU 2441: C++习题 复数类--重载运算符2+
- 104. Maximum Depth of Binary Tree
- C语言可变参实现参数累加返回
- C语言可变参实现参数累加返回
- 关于C和C++一些大神们的讨论
- Android adb.exe程序启动不起来,如何处理
- 深入浅出C++构造函数、析构函数、深浅拷贝的相关内容
- 数据结构:自组织线性表
- C语言可变参实现参数累加返回
- IOS-52-UITableView中cell分割线左对齐(解决iOS8版本适配问题)
- Tomcat服务器的配置
- uvaoj-232:crossword answer
- 关于C和C++一些大神们的讨论
- python小白-day6 面向对象编程
- python小白-day6 hashlib模块
- python小白-day6 ConfigParser模块
- python小白-day6 xml处理模块