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.
Input
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
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.
Examples
input
1 1
.
output
B
input
2 2
..
..
output
BW
WB
input
3 3
.-.
---
--.
output
B-B
---
--B
题意
给你一个矩阵,’-‘表示这个地方不能填字符,’.‘表示可以填字符。
你现在手上只有两种字符’B’和’W’,你要将’.‘填满字符,且相邻(上下左右)的元素之间不能为同一字符。
思路
dfs,从’.‘开始dfs,每一次找上下左右为’.'的字符,然后用一个bool变量保证所填的为不相同字符。
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define IOS ios::sync_with_stdio(false)
//#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, m;
char mp[110][110];
int dx[] = { -1,0,0,1 };
int dy[] = { 0,1,-1,0 };
void dfs(int x, int y, bool t)
{
mp[x][y] = (t ? 'B' : 'W');
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && mp[nx][ny] == '.')
{
dfs(nx, ny, t ^ 1);
}
}
}
int main()
{
IOS;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> mp[i][j];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (mp[i][j] == '.')
{
dfs(i, j, 1);
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cout << mp[i][j];
}
cout << endl;
}
}