Counting Black --- 暴力做法

总时间限制:
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).
Counting Black --- 暴力做法_第1张图片
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



解题分析:

  1. 对于一个100*100棋盘,进行下列三种操作 
  2. BLACK x y l  将(x,y)为左上角,边长为l的正方形涂黑 
  3. WHITE x y l  将(x,y)为左上角,边长为l的正方形涂白 
  4. TEST x y l 问(x,y)为左上角,边长为l的正方形有多少个黑色格子 
  5. 【输入】 
  6. 第一行一个数字t,表示操作次数 
  7. 接下来每行一个命令
  8. 【输出】 
  9. 每行回答一次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; 
}

Counting Black --- 暴力做法_第2张图片

你可能感兴趣的:(Counting Black --- 暴力做法)