CF445A DZY Loves Chessboard (#搜索 -1.7)

题意翻译

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

输入输出格式

输入格式:

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

Each of the next nn lines contains a string of mm characters: the jj -th character of the ii -th string is either "." or "-". A "." means that the corresponding cell (in the ii -th row and the jj -th column) is good, while a "-" means it is bad.

输出格式:

Output must contain nn lines, each line must contain a string of mm characters. The jj -th character of the ii -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

B

输入样例#2

2 2
..
..

输出样例#2

BW
WB

输入样例#3

3 3
.-.
---
--.

输出样例#3

B-B
---
--B

Note:

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

In the second sample, all 44 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 33 chessmen, no matter what their colors are.


思路

这题本来可以纯模拟做的,但是本蒟蒻正在学习搜索,所以我们就用dfs来做吧。

给出一张n*m的图,要在'.'的位置上填B或者W,给出要求B不能和B相邻,W不能和W相邻(就是黑板棋盘)。

然后从第一个为'.'的坐标开始搜,至于坏掉的格子我们不管它,因为不需要处理。

#include <stdio.h>
#include <iostream>
using namespace std;
int tox[5]={0,1,0,-1,0};//这是4个方向 
int toy[5]={0,0,1,0,-1};//这也是(话说学过dfs的都知道吧) 
char a[101][101];
int n,m;
inline void dfs(int x,int y,int i)//当前坐标x,y,以及应该放什么样的棋子i 
{
	register int j;
	if(x<0 || x>n || y<0 || y>m)//如果越过了地图边界 
	{
		return;//返回上一层 
	}
	if(i==0)//如果i为0 
	{
		a[x][y]='W';//放w 
	}
	else//反之亦然 
	{
		a[x][y]='B';
	}
	for(j=1;j<=4;j++)//搜索4个方向 
	{
		int x1=x+tox[j];//试探下一个坐标 
		int y1=y+toy[j];
		if(a[x1][y1]=='.')//这个点为'.' 
		{
			if(i==0)//如果x,y是W,则接下来要放B 
			{
				dfs(x1,y1,1);//继续搜下一个点,然后下一个点应该放B 
			}
			else//同上 
			{
				dfs(x1,y1,0);
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	register int i,j;
	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++)
		{//为什么不特判'-'的情况?因为我们针对这题只需要放W或B的棋子了,至于坏掉的格子我们不管,反正最后输出还是'-' 
			if(a[i][j]=='.')//从'.'开始搜 
			{
				dfs(i,j,1);
			}
		}
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			cout<<a[i][j];//打印地图 
		}
		cout<<endl;
	}
	cout<<endl;
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值