Counting Black --- 暴力做法
2015-10-18 00:54
369 查看
总时间限制: 1000ms内存限制: 65536kB描述There is a board with 100 * 100 grids as shown below. The left-top gird is denoted as (1, 1) and the right-bottom grid is (100, 100).
We may apply three commands to the board:
In the beginning, all the grids on the board are white. We apply a series of commands to the board. Your task is to write a program to give the numbers of black grids within a required region when a TEST command is applied.输入The first line of the input is an integer t (1 <= t <= 100), representing the number of commands. In each of the following lines, there is a command. Assume all the commands are legal which means that they won't try to paint/test the grids outside the board.输出For each TEST command, print a line with the number of black grids in the required region.样例输入
样例输出
来源
POJ Monthly--2004.05.15 Liu Rujia@POJ
解题分析:
对于一个100*100棋盘,进行下列三种操作
BLACK x y l 将(x,y)为左上角,边长为l的正方形涂黑
WHITE x y l 将(x,y)为左上角,边长为l的正方形涂白
TEST x y l 问(x,y)为左上角,边长为l的正方形有多少个黑色格子
【输入】
第一行一个数字t,表示操作次数
接下来每行一个命令
【输出】
每行回答一次TEST询问
对于这样的题目,很对大牛都选择了树状数组这种高级数据结构,我在这选择暴力做法。
每次读入选择利用字符读入,并且根据这三个单词的首字母不同,可以只判断首字母,然后进行相应的处理就可以。
We may apply three commands to the board:
1. WHITE x, y, L // Paint a white square on the board, // the square is defined by left-top grid (x, y) // and right-bottom grid (x+L-1, y+L-1) 2. BLACK x, y, L // Paint a black square on the board, // the square is defined by left-top grid (x, y) // and right-bottom grid (x+L-1, y+L-1) 3. TEST x, y, L // Ask for the number of black grids // in the square (x, y)- (x+L-1, y+L-1)
In the beginning, all the grids on the board are white. We apply a series of commands to the board. Your task is to write a program to give the numbers of black grids within a required region when a TEST command is applied.输入The first line of the input is an integer t (1 <= t <= 100), representing the number of commands. In each of the following lines, there is a command. Assume all the commands are legal which means that they won't try to paint/test the grids outside the board.输出For each TEST command, print a line with the number of black grids in the required region.样例输入
5 BLACK 1 1 2 BLACK 2 2 2 TEST 1 1 3 WHITE 2 1 1 TEST 1 1 3
样例输出
7 6
来源
POJ Monthly--2004.05.15 Liu Rujia@POJ
解题分析:
对于一个100*100棋盘,进行下列三种操作
BLACK x y l 将(x,y)为左上角,边长为l的正方形涂黑
WHITE x y l 将(x,y)为左上角,边长为l的正方形涂白
TEST x y l 问(x,y)为左上角,边长为l的正方形有多少个黑色格子
【输入】
第一行一个数字t,表示操作次数
接下来每行一个命令
【输出】
每行回答一次TEST询问
对于这样的题目,很对大牛都选择了树状数组这种高级数据结构,我在这选择暴力做法。
每次读入选择利用字符读入,并且根据这三个单词的首字母不同,可以只判断首字母,然后进行相应的处理就可以。
//杨鑫 #include <iostream> #include <cstdio> #include <cstring> int a[105][105]; int main() { int x,y,L,T; char str[10]; scanf("%d",&T); while(T--) { int s=0; scanf("%s",str); scanf("%d %d %d",&x,&y,&L); if(str[0]=='W') { for(int i=x;i<=x+L-1;i++) { for(int j=y;j<=y+L-1;j++) { a[i][j]=0; } } } else if(str[0]=='B') { for(int i=x;i<=x+L-1;i++) { for(int j=y;j<=y+L-1;j++) { a[i][j]=1; } } } else { for(int i=x;i<=x+L-1;i++) { for(int j=y;j<=y+L-1;j++) { if(a[i][j]==1) s=s+1; } } printf("%d\n",s); } } return 0; }
相关文章推荐
- 信息安全系统设计基础第五周学习总结
- OC常用数据类型大全解
- 2015 NEERC F. Gourmet and Banquet
- 在HTML5里用Ajax上传文件到Java Servlet
- 写给对独立开发的建议
- RPC 原理
- STL中algorithm里的查找
- 黑马程序员-Collection集合
- STL中algorithm里的查找
- python_12(查找字符串、字符串查找、时间转换、正则、限定符、分组)
- [自动运维]weblogic自动发布
- Struts2中 JSP使用request.setAttribute()后,在Action中为什么取不到值?
- KVC\KVO
- 51nod 1090 3个数和为0 (二分_stl)
- Mean Shift算法
- 热烈庆祝杨学明老师为苏宁、中兴、烽火、CNNIC、创维、金立、中航信等知名企业提供培训和咨询服务!
- iOS基础学习路线:内容,资料,资源
- 黑马程序员-Map集合部分
- Dijkstra-POJ-2502-Subway
- [uwp开发]数据绑定那些事(2)