poj1656---数黑格子

题意:有white,black,test操作

black将给定范围涂黑,white将给定范围涂白,test将给定范围的黑格子数出来并且输出

思路:无论哪个操作格子范围都在  (x,y)  (x+L-1,y+L-1),行跨度:x到x+L-1,列跨度:y到y+L-1

一个white操作,使用两个for循环对这一范围内的元素赋值,1位黑,0为白

以上行列跨度为1开始的,如果想直接引用,100*100数组最上面增加1行,最左边增加一列

arr[101][101],可以直接引用

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int table[101][101],row,col;
void white(int x,int y,int L)
{
    for(row=x;row<=(x+L-1);row++)
    {
        for(col=y;col<=(y+L-1);col++)
        {
            table[row][col]=0;
        }
    }
}
void black(int x, int y ,int L)
{
    for(row=x;row<=(x+L-1);row++)
    {
        for(col=y;col<=(y+L-1);col++)
        {
            table[row][col]=1;
        }
    }
}

int test(int x, int y ,int L)
{
    int count=0;
    for(row=x;row<=(x+L-1);row++)
    {
        for(col=y;col<=(y+L-1);col++)
        {
            if(table[row][col]==1)
                count++;
        }
    }
    return count;
}


int main()
{
    int n,x,y,L,count;
    char str[8];
    memset(table,0,sizeof(table));
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s%d%d%d",str,&x,&y,&L);
        if(strcmp(str,"WHITE")==0)
        {
            white(x,y,L);
        }
        else if(strcmp(str,"BLACK")==0)
        {
            black(x,y,L);
        }
        else
        {
            count=test(x,y,L);
            printf("%d
",count);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/gabygoole/p/4495359.html