CF445A DZY Loves Chessboard 题解

DZY Loves Chessboard

题面翻译

一个棋盘上有一些格子是坏的,另一些是正常的。对于每一个正常的格子,都要在上面放上棋子。
请找到一组解使没有两个相同颜色的棋子相邻(两个格子相邻为它们存在共同的边)
输入格式:
第一行为两个数n,m。(1<=n,m<=100)
下面n行,每个格子上的字符为’-‘或’.‘,’-‘表示坏掉的格子,’.'表示正常的格子。
输出格式:
输出n行,每行m个字符。第i个字符串的第j个字符应为“W”,“B”或“ - ”。字符“W”是指在格子上放白色的棋子,“B”意味着放黑色的棋子,“ - ”表示坏掉的格子。
如果有多组答案,输出其中的一个

感谢@zhaotiensn 提供的翻译

题目描述

DZY loves chessboard, and he enjoys playing with it.

He has a chessboard of $ n $ rows and $ m $ columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.

You task is to find any suitable placement of chessmen on the given chessboard.

输入格式

The first line contains two space-separated integers $ n $ and $ m $ $ (1<=n,m<=100) $ .

Each of the next $ n $ lines contains a string of $ m $ characters: the $ j $ -th character of the $ i $ -th string is either “.” or “-”. A “.” means that the corresponding cell (in the $ i $ -th row and the $ j $ -th column) is good, while a “-” means it is bad.

输出格式

Output must contain $ n $ lines, each line must contain a string of $ m $ characters. The $ j $ -th character of the $ i $ -th string should be either “W”, “B” or “-”. Character “W” means the chessman on the cell is white, “B” means it is black, “-” means the cell is a bad cell.

If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.

样例 #1

样例输入 #1

1 1
.

样例输出 #1

B

样例 #2

样例输入 #2

2 2
..
..

样例输出 #2

BW
WB

样例 #3

样例输入 #3

3 3
.-.
---
--.

样例输出 #3

B-B
---
--B

提示

In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.

In the second sample, all $ 4 $ cells are good. No two same chessmen share an edge in the sample output.

In the third sample, no good cells are adjacent. So you can just put $ 3 $ chessmen, no matter what their colors are.

#include<bits/stdc++.h>
using namespace std;
inline int read();
long long i,q,w,e,r,t,y,u,o,p,s,d,f,g,h,j,l,z,x,c,v,n,m,k;
char a[1100][1100],b[1100][1100];
struct aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
{
	int xx;int yy;int zz;
}aa[161000];
int main()
{
	ios::sync_with_stdio(false); cout.tie(0); cin.tie(0);
	cin>>n>>m;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}	
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			for(k=1;k<=r+5;k++)
			{
				aa[k].xx=0;
				aa[k].yy=0;
			}
			if(a[i][j]=='.')
			{
				aa[1].zz=0;
				a[i][j]='B';
				aa[1].xx=i;aa[1].yy=j;
				l=1;r=2;
				while(l<r)
				{
					if(a[aa[l].xx+1][aa[l].yy]=='.')
					{
						aa[r].xx=aa[l].xx+1;
						aa[r].yy=aa[l].yy;
						aa[r].zz=aa[l].zz+1;
						if(aa[r].zz%2==1) a[aa[l].xx+1][aa[l].yy]='W';
						else if(aa[r].zz%2==0) a[aa[l].xx+1][aa[l].yy]='B';
						r++;
					}
					if(a[aa[l].xx-1][aa[l].yy]=='.')
					{
						aa[r].xx=aa[l].xx-1;
						aa[r].yy=aa[l].yy;
						aa[r].zz=aa[l].zz+1;
						if(aa[r].zz%2==1) a[aa[l].xx-1][aa[l].yy]='W';
						else if(aa[r].zz%2==0) a[aa[l].xx-1][aa[l].yy]='B';
						r++;
					}
					if(a[aa[l].xx][aa[l].yy+1]=='.')
					{
						aa[r].xx=aa[l].xx;
						aa[r].yy=aa[l].yy+1;
						aa[r].zz=aa[l].zz+1;
						if(aa[r].zz%2==1) a[aa[l].xx][aa[l].yy+1]='W';
						else if(aa[r].zz%2==0) a[aa[l].xx][aa[l].yy+1]='B';
						r++;
					}
					if(a[aa[l].xx][aa[l].yy-1]=='.')
					{
						aa[r].xx=aa[l].xx;
						aa[r].yy=aa[l].yy-1;
						aa[r].zz=aa[l].zz+1;
						if(aa[r].zz%2==1) a[aa[l].xx][aa[l].yy-1]='W';
						else if(aa[r].zz%2==0) a[aa[l].xx][aa[l].yy-1]='B';
						r++;
					}
				    l++;
				}
			}
		}
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			cout<<a[i][j];
		}
		cout<<endl;
	}	
	
    return 0;
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只贴代码君

帅帅的你,留下你的支持吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值