您的位置:首页 > 其它

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:
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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: