hdu 4146 Flip Game
来源:互联网 发布:淘宝上有卖少女脚皮的 编辑:程序博客网 时间:2024/06/10 20:40
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4146
Flip Game
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1789 Accepted Submission(s): 585
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.
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).
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
24bwwwwbwwwwbwwwwb21 14 44wwwwwwwwwwwwwwww11 1
Sample Output
Case #1: 4Case #2: 10
Author
Hzwu@SWJTU
Source
2011百校联动“菜鸟杯”程序设计公开赛
解题思路:这个题目中时间要求特别严格,超时n多次~~~需要采用位运算进行次数的处理。(如果翻转的次数为偶数次就不需要进行处理,因为翻转了也要在翻转回来)
还要就是输入!!!刚开始采用的是字符输入的超时!改成字符串输入A掉了,祝你们好运~~~~
详见代码。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char ch[1010][1010];int vh[1010],vl[1010];int n;int main(){ int t; int q=1; scanf("%d",&t); while (t--) { scanf("%d",&n); memset(vh,0,sizeof(vh)); memset(vl,0,sizeof(vl)); for (int i=1; i<=n; i++) { scanf("%s",ch[i]+1); //getchar(); //for (int j=1; j<=n; j++) //{ // ch[i][j]=getchar(); //} } int k; scanf("%d",&k); while (k--) { int x,y; scanf("%d%d",&x,&y); vh[x]^=1; vl[y]^=1; } int ans=0; for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++) { if(vh[i]+vl[j]==1) { if (ch[i][j]=='b') ans++; } else if (ch[i][j]=='w') ans++; } } //return ans; //turn(); printf ("Case #%d: %d\n",q++,ans); } return 0;}
0 0
- HDU 4146 Flip Game
- HDU 4146 Flip game
- hdu 4146 Flip Game
- hdu 4146 Flip Game【思维+暴力】
- HDOJ 4146 Flip Game
- hdoj 4146(Flip Game)
- HDU 4146Flip Game (思考 找规律)
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- flip game
- Flip Game
- Flip Game
- FLIP GAME
- Flip Game
- UILabel / UITextView load HTML文本
- Cocos2d-x 3.x数据存储(UserDefault)
- Leetcode_299_Bulls and Cows
- Android事件机制
- 通过java.net.URLConnection发送HTTP请求的方法
- hdu 4146 Flip Game
- Unity5 弹力球的制作
- 蓝桥杯 三羊献瑞
- 2048
- android:Notification实现状态栏的通知
- 单身不能为空---领料单
- ZOJ 1023 University Entrace Examination(稳定匹配变形)
- DSP 28335 的中断系统总结
- 安卓通知的使用系列4:对话框通知的使用之列表对话框、单选列表对话框、多选列表对话框