Little Artem(Codeforce-1333A)

A. Little Artem
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that’s why she asked for your help.

Artem wants to paint an n×m board. Each cell of the board should be colored in white or black.

Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B=W+1.

The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color).
在这里插入图片描述

However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn’t have a neighbor with the opposite color).

Please, help Medina to find any good coloring. It’s guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them.

Each test contains multiple test cases.

The first line contains the number of test cases t (1≤t≤20). Each of the next t lines contains two integers n,m (2≤n,m≤100) — the number of rows and the number of columns in the grid.

For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with ‘B’ means that the cell is black, and ‘W’ means white). Do not use quotes.

It’s guaranteed that under given constraints the solution always exists.
Example
inputCopy
2
3 2
3 3
outputCopy
BW
WB
BB
BWB
BWW
BWB
Note
In the first testcase, B=3, W=2.

In the second testcase, B=5, W=4. You can see the coloring in the statement.
题意:
一个nm的图,有nm个小方块,由B和W组成,选择一种填图方式,使得B=W+1成立,输出这种填图,(保证一定有解)
思路:
我刚开始没看懂题,看了一个大佬的思路,自己又画了画,差不多理解了,就是一道找规律的题,毕竟是Div2的A题
(1)首先先按BW的顺序填满整个图
(2)然后你就能发现如果是奇数个格子就能满足B=W+1,前提是先填B 再填W
(3)如果是偶数个格子,它满足B=W,如果要变成B=W+1,B要比W大一个,因为第一个填的B,把第二个W改为B,你再数一下,就会发现满足B=W+1,也算试出来的吧。
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
int main ()
{
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d %d",&n,&m);
        char a[101][101];
        int ans=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(ans==0){//B,W顺序填图
                    a[i][j]='B';
                    ans=1;
                }
                else if(ans==1){
                    a[i][j]='W';
                    ans=0;
                }
            }
        }
        if((n*m)%2!=0){//奇数个直接输出就行
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    printf("%c",a[i][j]);
                }
                cout<<endl;
            }
        }
        else{//偶数个改第二个W为B就满足了要求
            int sum=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    sum++;
                    if(sum==2){
                        a[i][j]='B';
                        break;
                    }
                }
                if(sum==2) break;
            }
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    printf("%c",a[i][j]);
                }
                cout<<endl;
            }
        }
    }
    return 0;
}

给自己的建议:不要看到题看不懂就躲,多读几遍,自己多试试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值