[codeforces 1333A] Little Artem 读懂题+找规律+多举例

Codeforces Round #632 (Div. 2)   比赛人数12810

[codeforces 1333A]   Little Artem   读懂题+找规律+多举例

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1333/problem/A

Problem Lang Verdict Time Memory
A - Little Artem GNU C++17 Accepted 15 ms 0 KB

读完题,遇到了一个矛盾,样例中

3 2
BW
WB
BB

明明有4个B,2个W,并不符合B=W+1啊

但是文中的提示,却说In the first testcase, B=3, W=2.

怎么回事。借助翻译软件也没弄明白,反反复复读题,才发现

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.

白格子能找到相邻的黑格子,该白格子才能参与计数

符合条件的黑白格子总数量

B+W=W+1+W=2*W+1

很明显是奇数,

若格子总数量(n*m)是奇数,就让B,W间隔出现,有了如下例子,该中情况就比较好编了,细节可见代码。

3 3
BWB
WBW
BWB

3 5
BWBWB
WBWBW
BWBWB
WBWBW

若格子总数量(n*m)是偶数,就让B,W间隔出现,就需多举些例子来找规律。同样有了如下例子(请注意第2行的第1个字母是B,第2行的第2个字母是B),该种情况就比较好编了,细节可见代码。

2 2

Bw
BB

2 3
BWB
BBW

2 4
BWBW
BBBW
BWBW

2 5
BWBWB
BBWBW

3 2
BW
BB
BW

AC代码如下

#include <stdio.h>
int main(){
	int t,n,m,a,b,i,line;
	char c;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		a=n*m;
		if(a%2){
			for(i=0;i<a;i++){
				if(i>0&&i%m==0)printf("\n");//换行
				if(i%2==0)c='B';
				else c='W';
				printf("%c",c);
			}
			printf("\n");//换行
		}else{
			line=0;
			for(i=0;i<a;i++){
				if(i>0&&i%m==0)line++,printf("\n");//换行
				if(i%2==0)c='B';
				else c='W';
				if(line==1&&i%m==0)c='B';//第2行开始的第1个字母处理
				if(line==1&&i%m==1)c='B';//第2行开始的第2个字母处理
				printf("%c",c);
			}
			printf("\n");//换行
		}
	}
	return 0;
}

比赛的第一题,不菜啊。

发布了660 篇原创文章 · 获赞 562 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/105402588