hdu 4146 Flip Game【思维+暴力】
2016-04-03 17:15
260 查看
Flip Game
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1839 Accepted Submission(s): 604
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
暴力敲了两发觉得5000ms时间已经放的很宽了,差不多能过,但是5000ms总归扛不住20*10^8的复杂度。
正确思路:标记上第i行有多少次flip,标记上第i列有多少次flip。然后遍历行和列,对于第i行操作为奇数的情况,直接相当于操作一次,对于操作为偶数的情况,直接相当于没有操作过,这样模拟就从20*10^8变成了20*3*10^6,是一定能过了的。
AC代码:
#include<stdio.h> #include<string.h> using namespace std; char a[1005][1005]; int col[1005]; int row[1005]; int main() { int t; int kase=0; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); memset(row,0,sizeof(row)); memset(col,0,sizeof(col)); for(int i=1;i<=n;i++) { scanf("%s",a[i]+1); } int q; scanf("%d",&q); while(q--) { int x,y; scanf("%d%d",&x,&y); row[x]++; col[y]++; } for(int i=1;i<=n;i++) { if(row[i]%2==0)continue; else { for(int j=1;j<=n;j++) { if(a[i][j]=='w')a[i][j]='b'; else a[i][j]='w'; } } } for(int i=1;i<=n;i++) { if(col[i]%2==0)continue; else { for(int j=1;j<=n;j++) { if(a[j][i]=='w')a[j][i]='b'; else a[j][i]='w'; } } } int output=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(a[i][j]=='w')output++; } } printf("Case #%d: %d\n",++kase,output); } }
相关文章推荐
- Linux内核创建一个新进程的过程
- fork和exec函数
- 获得同级iframe页面的指定ID元素的几种实现方法
- 深入理解letter-spacing和word-spacing2
- 空闲时间继续来学习!!!!!
- linux -grep用法
- 小黑的日常折腾-网段在线地址扫描shell脚本
- JavaScript之jQuery-7 jQuery 使用插件(使用插件、日历插件、表单验证插件)
- 最成功的失败学——《创游记:游戏团队创业成长之路》
- ACM常用的C++头文件!
- Java设计模式之单例模式
- Java并发编程系列之十一:synchronized(2)
- jvm(12)-java内存模型与线程
- [CodeVs1050]棋盘染色2(状态压缩DP)
- Android Linux内核编译调试
- 国际信息学奥林匹克竞赛(IOI)竞赛语言由三门变为五门
- Android设计模式应用--工厂模式
- HDU4331 Image Recognition
- 环境配置中path和classpath的作用
- leetcode之Power of Two